Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix django admin EmbeddedField CRUD #623

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions djongo/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ def to_python(self, value):
if isinstance(value, str):
value = json.loads(value)

# On POST, a Model Object was input to this method.
if isinstance(value, self.model_container):
value = {field.attname: getattr(value, field.attname)
for field in value._meta.fields}

if not isinstance(value, self.base_type):
raise ValidationError(
f'Value: {value} must be an instance of {self.base_type}')
Expand Down Expand Up @@ -590,6 +595,13 @@ class EmbeddedFormBoundField(forms.BoundField):
# return getattr(self.field.model_form, name)

def __str__(self):
empty_model = self.field.model_form._meta.model
instance = self.value()
if instance:
# The model_form_class expects a Model object.
for key, val in instance.items():
setattr(empty_model, key, val)
instance = empty_model
instance = self.value()
model_form = self.field.model_form_class(instance=instance, **self.field.model_form_kwargs)

Expand All @@ -608,6 +620,9 @@ def decompress(self, value):
return value
elif isinstance(value, Model):
return [getattr(value, f_n) for f_n in self.field_names]
elif isinstance(value, dict):
# On update, a dict was input into here.
return value
else:
raise forms.ValidationError('Expected model-form')

Expand Down