Open Vayel opened 7 years ago
@Vayel Hi, Vincent!
Do you realize, that normally you don't have to create serializers for EmbeddedDocument
s and fields for serializers by hand?
So, the following code should be enough:
class Embedded(EmbeddedDocument):
date = fields.DateTimeField(required=True,)
class MyDoc(Document):
children = fields.MapField(fields.EmbeddedDocumentField('Embedded'))
class MyDocSerializer(mongoserializers.DocumentSerializer):
class Meta:
model = MyDoc
fields = '__all__'
DocumentSerializer
creates all the necessary fields and EmbeddedSerializer
s itself.
@BurkovBA Yes, I do. It is a minimal example. What I really want to do is:
class EmbeddedSerializer(mongoserializers.EmbeddedDocumentSerializer):
date = serializers.DateTimeField(format="%Y-%m-%d", input_formats=["%Y-%m-%d"]) # Bypass the absence of mongoengine.DateField
class Meta:
model = Embedded
fields = '__all__'
@Vayel
Oh, but there is a DateTimeField
in Mongoengine and it is automatically mapped to DRF's DateTimeField
. Is it missing some functionality, you're looking for?
@BurkovBA I would like to use the DateTimeField
as a DateField
(for a document publication date, I do not care about hours and minutes), that is to be able to make such a request:
{
"children": {
"key": {
"date": "2016-05-05"
}
}
}
@Vayel
Ah, I see. Well, I'll have to jump into debugger with this one to figure out, what went wrong. I'll write about my results, when I'm done.
@Vayel
Took me a while to figure out: don't import DictFields from serializers
- I had to override DictField
in DRF-ME to support parsing mongoengine.MapField
arguments into DictField
options. Instead, use rest_framework_mongoengine.fields.DictField
. Sorry about the confusion.
@BurkovBA
It works, thanks a lot!
Just in case someone would like to use DateTimeField
as a DateField
, I think the cleanest way is to use extra_kwargs
:
class CollectedDocumentSerializer(mongoserializers.EmbeddedDocumentSerializer):
class Meta:
model = models.CollectedDocument
fields = '__all__'
# Should be in CollectionSerializer.Meta.extra_kwargs ("documents.child.xxx"),
# but it does not work yet. See #215
extra_kwargs = {
'collected_on': {
'format': '%Y-%m-%d',
'input_formats': ['%Y-%m-%d'],
},
'description': {'allow_blank': True},
}
class CollectionSerializer(mongoserializers.DocumentSerializer):
documents = mongofields.DictField(child=CollectedDocumentSerializer(), required=False,)
class Meta:
model = models.Collection
fields = '__all__'
read_only_fields = ('created_on', 'updated_on',)
extra_kwargs = {
'created_on': {'format': '%Y-%m-%d'},
'updated_on': {'format': '%Y-%m-%d'},
}
Hi!
I have the following code:
If I remove the redefinition of
MyDocSerializer.children
, it works well. But with the code above, I obtain the following error on a PUT (or PATCH) request (sent from the browsable api):I have no errors with POST:
Thanks