django-rest-framework-mongoengine
django-rest-framework-mongoengine copied to clipboard
Write unit-test that shows incorrect handling of overridden id field.
Suppose that I've overridden id
field on a Document with an arbitrary datatype (non-ObjectId) and created a default serializer for it:
class MyDocument(Document):
id = StringField(primary_key=True, required=True)
class MySerializer(DocumentSerializer):
class Meta:
model = MyDocument
Currently, DRF-M handles this situation incorrectly. If I pass data={id: "SomeUniqueKey"} to MySerializer, it successfully stores id
key-value pair in initial_data
, but not in validated_data
. Still, it successfully passes is_valid()
, but does not create a value for id
in me_data
during recrusive_save()
and fails mongoengine validation, when trying to create a model instance in instance = self.Meta.model(**me_data)
.
I have to do the following:
- [x] Write a unit-test that points to this incorrect behavior
- [ ] Fix this behavior
- [ ] Write a unit-test that points to incorrect hadling of custom id field be ReferenceFields
- [ ] Fix this behavrior
Also, DocumentSerializer uses ReferenceField without care about type of referenced model id. It can be adjusted on class level globally, but automagic will fail.
@qwiglydee Ah, I didn't see that, thanks for suggestion. Qwiglydee, while you're still here, I have a couple of design questions:
- Are DynamicEmbeddedDocuments meant to work with normal EmbeddedDocumentSerializer, or should we create another specific branch in
build_field()
, analogous tobuild_nested_embedded_field()
, where we'll create a specific dynamic EmbeddedDocumentSerializer subclass, as in https://github.com/umutbozkurt/django-rest-framework-mongoengine/blob/master/rest_framework_mongoengine/serializers.py#L464? - Do we handle
extra_kwargs
for fields in nestedEmbeddedDocumentSerializers
, that were created automatically? E.g. something like this:
class Kid(EmbeddedDocumentSerializer):
foo = StringFIeld()
class Papa(Document):
kid = EmbeddedDocumentField(Kid)
class PapaSerializer(DocumentSerializer):
class Meta:
model = Papa
extra_kwargs={
'kid.foo': {allow_blank: True}
}