django-rest-framework-mongoengine icon indicating copy to clipboard operation
django-rest-framework-mongoengine copied to clipboard

Write unit-test that shows incorrect handling of overridden id field.

Open BurkovBA opened this issue 8 years ago • 2 comments

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

BurkovBA avatar Jul 07 '16 15:07 BurkovBA

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 avatar Jul 07 '16 16:07 qwiglydee

@qwiglydee Ah, I didn't see that, thanks for suggestion. Qwiglydee, while you're still here, I have a couple of design questions:

  1. Are DynamicEmbeddedDocuments meant to work with normal EmbeddedDocumentSerializer, or should we create another specific branch in build_field(), analogous to build_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?
  2. Do we handle extra_kwargs for fields in nested EmbeddedDocumentSerializers, 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}
        }

BurkovBA avatar Jul 07 '16 16:07 BurkovBA