django rest framework and embedded EmbeddedModelField
I really like the feature that making a model abstract we can avoid creating separate collections, since it makes sense to me to create collections for reusable documents only. However, I've been having a hard time trying to serialize those models with DRF. Is there any specific documentation on the matter?
Lets say your models look like this:
class MyEmbeddedModel(models.Model):
...
class Meta:
abstract = True
class MyModel(models.Model):
my_embedded_model = models.EmbeddedModelField(model=MyEmbeddedModel)
objects = models.DjongoManager()
(or something like that)
Then you can do:
from bson.json_util import dumps
# MyModel is the parent
@api_view(['GET'])
def my_view(request):
query = MyModel.objects.mongo_find()
return dumps(query)
This should be a really fast, because you're using the native converter. It's at least my solution to the problem.
Thank you very much!
@MiltosD This is how I use Djongo with DRF - This handles both EmbeddedModelFields and ArrayModelFields. Define a serializer for the model with the embedded field in the following way:
# Let's say the name of the embedded field in the ParentModel is "embedded_field"
class SampleSerializer(serializers.ModelSerializer):
# You need to define a SerializerMethodField for every embedded model field in the ParentModel
embedded_field = serializers.SerializerMethodField()
class Meta:
model = ParentModel
fields = ('id', 'title', 'embedded_field')
def get_embedded_field(self, obj):
return_data = None
if type(obj.embedded_field) == list:
embedded_list = []
for item in field:
embedded_dict = item.__dict__
for key in list(embedded_dict.keys()):
if key.startswith('_'):
embedded_dict.pop(key)
embedded_list.append(embedded_dict)
return_data = embedded_list
else:
embedded_dict = field.__dict__
for key in list(embedded_dict.keys()):
if key.startswith('_'):
embedded_dict.pop(key)
return_data = embedded_dict
return return_data
field in field.__dict__ is actually obj.embedded_field for those that might be confused with @arindammanidas' solution.
@arindammanidas: Great!!
Thank you for this post. Helped me get over the issue!
@MiltosD This is how I use Djongo with DRF - This handles both EmbeddedModelFields and ArrayModelFields. Define a serializer for the model with the embedded field in the following way:
# Let's say the name of the embedded field in the ParentModel is "embedded_field" class SampleSerializer(serializers.ModelSerializer): # You need to define a SerializerMethodField for every embedded model field in the ParentModel embedded_field = serializers.SerializerMethodField() class Meta: model = ParentModel fields = ('id', 'title', 'embedded_field') def get_embedded_field(self, obj): return_data = None if type(obj.embedded_field) == list: embedded_list = [] for item in field: embedded_dict = item.__dict__ for key in list(embedded_dict.keys()): if key.startswith('_'): embedded_dict.pop(key) embedded_list.append(embedded_dict) return_data = embedded_list else: embedded_dict = field.__dict__ for key in list(embedded_dict.keys()): if key.startswith('_'): embedded_dict.pop(key) return_data = embedded_dict return return_data
How to post data ?
@arindammanidas I am getting errror with this that 'NoneType' object has no attribute 'keys'
@MiltosD This is how I use Djongo with DRF - This handles both EmbeddedModelFields and ArrayModelFields. Define a serializer for the model with the embedded field in the following way:
# Let's say the name of the embedded field in the ParentModel is "embedded_field" class SampleSerializer(serializers.ModelSerializer): # You need to define a SerializerMethodField for every embedded model field in the ParentModel embedded_field = serializers.SerializerMethodField() class Meta: model = ParentModel fields = ('id', 'title', 'embedded_field') def get_embedded_field(self, obj): return_data = None if type(obj.embedded_field) == list: embedded_list = [] for item in field: embedded_dict = item.__dict__ for key in list(embedded_dict.keys()): if key.startswith('_'): embedded_dict.pop(key) embedded_list.append(embedded_dict) return_data = embedded_list else: embedded_dict = field.__dict__ for key in list(embedded_dict.keys()): if key.startswith('_'): embedded_dict.pop(key) return_data = embedded_dict return return_data
With above solution also im getting an error when i try to create model with Embeddedfields within it from django Admin. Says the EmbeddedFiled should be a instance of dict. "Value: Design must be an instance of <class 'dict'>"
@MiltosD This is how I use Djongo with DRF - This handles both EmbeddedModelFields and ArrayModelFields. Define a serializer for the model with the embedded field in the following way:
# Let's say the name of the embedded field in the ParentModel is "embedded_field" class SampleSerializer(serializers.ModelSerializer): # You need to define a SerializerMethodField for every embedded model field in the ParentModel embedded_field = serializers.SerializerMethodField() class Meta: model = ParentModel fields = ('id', 'title', 'embedded_field') def get_embedded_field(self, obj): return_data = None if type(obj.embedded_field) == list: embedded_list = [] for item in field: embedded_dict = item.__dict__ for key in list(embedded_dict.keys()): if key.startswith('_'): embedded_dict.pop(key) embedded_list.append(embedded_dict) return_data = embedded_list else: embedded_dict = field.__dict__ for key in list(embedded_dict.keys()): if key.startswith('_'): embedded_dict.pop(key) return_data = embedded_dict return return_dataWith above solution also im getting an error when i try to create model with Embeddedfields within it from django Admin. Says the EmbeddedFiled should be a instance of dict. "Value: Design must be an instance of <class 'dict'>"
how to fix this
Same error
@MiltosD This is how I use Djongo with DRF - This handles both EmbeddedModelFields and ArrayModelFields. Define a serializer for the model with the embedded field in the following way:
# Let's say the name of the embedded field in the ParentModel is "embedded_field" class SampleSerializer(serializers.ModelSerializer): # You need to define a SerializerMethodField for every embedded model field in the ParentModel embedded_field = serializers.SerializerMethodField() class Meta: model = ParentModel fields = ('id', 'title', 'embedded_field') def get_embedded_field(self, obj): return_data = None if type(obj.embedded_field) == list: embedded_list = [] for item in field: embedded_dict = item.__dict__ for key in list(embedded_dict.keys()): if key.startswith('_'): embedded_dict.pop(key) embedded_list.append(embedded_dict) return_data = embedded_list else: embedded_dict = field.__dict__ for key in list(embedded_dict.keys()): if key.startswith('_'): embedded_dict.pop(key) return_data = embedded_dict return return_dataWith above solution also im getting an error when i try to create model with Embeddedfields within it from django Admin. Says the EmbeddedFiled should be a instance of dict. "Value: Design must be an instance of <class 'dict'>"
how to fix this
I have this same issue , can someone help fixing it ?
@MiltosD This is how I use Djongo with DRF - This handles both EmbeddedModelFields and ArrayModelFields. Define a serializer for the model with the embedded field in the following way:
# Let's say the name of the embedded field in the ParentModel is "embedded_field" class SampleSerializer(serializers.ModelSerializer): # You need to define a SerializerMethodField for every embedded model field in the ParentModel embedded_field = serializers.SerializerMethodField() class Meta: model = ParentModel fields = ('id', 'title', 'embedded_field') def get_embedded_field(self, obj): return_data = None if type(obj.embedded_field) == list: embedded_list = [] for item in field: embedded_dict = item.__dict__ for key in list(embedded_dict.keys()): if key.startswith('_'): embedded_dict.pop(key) embedded_list.append(embedded_dict) return_data = embedded_list else: embedded_dict = field.__dict__ for key in list(embedded_dict.keys()): if key.startswith('_'): embedded_dict.pop(key) return_data = embedded_dict return return_dataWith above solution also im getting an error when i try to create model with Embeddedfields within it from django Admin. Says the EmbeddedFiled should be a instance of dict. "Value: Design must be an instance of <class 'dict'>"
Exactly same error here.
Can you show the model clase code ? Embeded_field default value? Null =true?
El vie., 30 de oct. de 2020 6:10 p. m., Bruno Wego [email protected] escribió:
@MiltosD https://github.com/MiltosD This is how I use Djongo with DRF - This handles both EmbeddedModelFields and ArrayModelFields. Define a serializer for the model with the embedded field in the following way:
Let's say the name of the embedded field in the ParentModel is "embedded_field"
class SampleSerializer(serializers.ModelSerializer): # You need to define a SerializerMethodField for every embedded model field in the ParentModel embedded_field = serializers.SerializerMethodField() class Meta: model = ParentModel fields = ('id', 'title', 'embedded_field')
def get_embedded_field(self, obj): return_data = None if type(obj.embedded_field) == list: embedded_list = [] for item in field: embedded_dict = item.__dict__ for key in list(embedded_dict.keys()): if key.startswith('_'): embedded_dict.pop(key) embedded_list.append(embedded_dict) return_data = embedded_list else: embedded_dict = field.__dict__ for key in list(embedded_dict.keys()): if key.startswith('_'): embedded_dict.pop(key) return_data = embedded_dict return return_dataWith above solution also im getting an error when i try to create model with Embeddedfields within it from django Admin. Says the EmbeddedFiled should be a instance of dict. "Value: Design must be an instance of <class 'dict'>"
Exactly same error here.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/nesdis/djongo/issues/115#issuecomment-719801242, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL2EH6HXDPR4MG5LLHV63N3SNMTU5ANCNFSM4E3P3ZLA .
@antonio-irarrazaval take a look https://github.com/nesdis/djongo/issues/491