From 462ac52a57c87001eddfc15102dca7d3076d17a5 Mon Sep 17 00:00:00 2001 From: snphan Date: Sat, 11 Jun 2022 01:20:38 -0600 Subject: [PATCH] fix django admin CRUD for EmbeddedField --- djongo/models/fields.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/djongo/models/fields.py b/djongo/models/fields.py index 87a57d6b..a5fc36a9 100644 --- a/djongo/models/fields.py +++ b/djongo/models/fields.py @@ -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}') @@ -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) @@ -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')