1đź‘Ť
On my model I had overridden clean_fields
. The signature for clean_fields
:
Model.clean_fields(exclude=None)
My custom clean_fields
:
def clean_fields(self, exclude=None):
super(Applicant, self).clean_fields() # Bug here.
# custom clean_fields code
I didn’t pass the exclude
parameter to the super class!
Oh god this took me so many hours to debug.
1đź‘Ť
Check DRF 3.0 release notes docs. They have an example doing exactly what you need with perform_create()
in ViewSet .
- [Answered ]-Is it possible to generically access the response object in a Django Rest Framework ModelViewSet?
- [Answered ]-How to make a Model with a Many-to-Many relationship as Categories (i.e) rock ,Country, Blues, and Pop
- [Answered ]-Use variable as field in ORM query
0đź‘Ť
Include the “owner” field in the serializer Meta.exclude
(or just remove it from the field list if you are using the “positive” list).
class FooSerializer(serializers.Serializer):
class Meta(object):
model: Foo
exclude: ('owner',)
This way you will not trigger validation for this field. Also, make sure the endpoint is decorated with mandatory authentication or check if self.request.user.is_authenticated()
is True
.
0đź‘Ť
The problem seems to be that the validation fails because the “owner” field is required but not given a value in the initial data(it gets a value after validation). Because serializer.is_valid() is run before pre_save() then it fails.
Why the version change fixes it I have no idea. I myself have set owner as required=False although an object always will get one from pre_save()
- [Answered ]-AWS – DJANGO – Error in formatting: OperationalError: fe_sendauth: no password supplied
- [Answered ]-Django fastest request between query and sub query