umutbozkurt / django-rest-framework-mongoengine

Mongoengine support for Django Rest Framework
MIT License
616 stars 166 forks source link

Fix Attribute error with Reference Field #217

Closed luckypur closed 7 years ago

luckypur commented 7 years ago

This PR fixes the following bug: Cant find model attribute on Reference field

i was doing serializer = MySerializer(my_queryset, many=True)

And not defining reference field explicitly in MySerializers.

So i am using related model instead(in the code)..which is the same

Error screenshot

pr1

BurkovBA commented 7 years ago

Hi, @luckypur

Thanks for your Pull Request.

Can you add a unit-test that demonstrates the error that you observed? Is it associated with ReferenceField? Then, add it to https://github.com/umutbozkurt/django-rest-framework-mongoengine/blob/master/tests/test_reference.py file, please.

luckypur commented 7 years ago

This Error is not related to Reference field i think. It comes when we dont explicitly declare the ReferenceField in serializer for all referenced field in our Document. So i that case it detects that some field is ReferenceField but there is no Model attribute.

BurkovBA commented 7 years ago

@luckypur I need more details on how you define your serializer and how you use it in your viewset, cause it might be either library bug or you using it wrong =) (which is ok, this library is quite complicated) . Normally, serializer and documents are declared as follows:

class MyDocument(mongoengine.Document):
    owner = mongoengine.fields.ReferenceField(User)
    text = mongoengine.fields.StringField()

class User(mongoengine.Document):
    name = mongoengine.fields.StringField()
    date_of_birth = mongoengine.fields.DateTimeField()

class MySerializer(rest_framework_mongoengine.serializers.DocumentSerializer):
    class Meta:
        model = MyDocument
        fields = ('__all__')

Can you provide an example of your code, so that I can reproduce the bug, you observed and trying to fix?

luckypur commented 7 years ago

Try making owner Unique to reproduce the bug. owner = mongoengine.fields.ReferenceField(User, unique=True)

luckypur commented 7 years ago

so do you want me to add a unit-test for that..!

BurkovBA commented 7 years ago

@luckypur If you could, yes, it would be super-awesome. Sorry, I have several deadlines on my full-time job this and next week, so I'm not very active currently. I'll also introduce 2 new features into DRF-ME - validation and extra_kwargs for nested fields - validate_parent__child() method and parent.child fields in kwargs.

Thanks a lot!

luckypur commented 7 years ago

Sorry for delay. Same with my full time job as well :). Have a look at new test case and let me know your thoughts.

BurkovBA commented 7 years ago

@luckypur

Awesome job, thanks a lot.

One request: we could've used a test that triggers a validation error, when you're trying to submit a reference to a document, which is already referenced by someone else, and unique validator raises an error.

Merged your PR. Cheers!

luckypur commented 7 years ago

Yeah...will do that soon if it goes fine with DRF...