[Answered ]-With django-rest-framework how do I assign a field to the request user when using a ModelViewSet?

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.

👤Dustin Wyatt

1đź‘Ť

Check DRF 3.0 release notes docs. They have an example doing exactly what you need with perform_create() in ViewSet .

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()

👤timop

Leave a comment