Closed luckypur closed 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.
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.
@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?
Try making owner Unique to reproduce the bug. owner = mongoengine.fields.ReferenceField(User, unique=True)
so do you want me to add a unit-test for that..!
@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!
Sorry for delay. Same with my full time job as well :). Have a look at new test case and let me know your thoughts.
@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!
Yeah...will do that soon if it goes fine with DRF...
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