umutbozkurt/django-rest-framework-mongoengine

Serializer object has no attribute '_writable_fields'

Closed this issue · 4 comments

I have started writing an app in django with mongodb (my first time). But I'm getting this error related to my DRF-mongoengine serializer. The error reads:

AttributeError: 'UserSerializer' object has no attribute '_writable_fields'

Full Traceback is as follows:

Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
web_1  |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
web_1  |     return view_func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python2.7/site-packages/rest_framework/viewsets.py", line 85, in view
web_1  |     return self.dispatch(request, *args, **kwargs)
web_1  |   File "/usr/local/lib/python2.7/site-packages/rest_framework/views.py", line 407, in dispatch
web_1  |     response = self.handle_exception(exc)
web_1  |   File "/usr/local/lib/python2.7/site-packages/rest_framework/views.py", line 404, in dispatch
web_1  |     response = handler(request, *args, **kwargs)
web_1  |   File "/usr/local/lib/python2.7/site-packages/rest_framework/mixins.py", line 20, in create
web_1  |     serializer.is_valid(raise_exception=True)
web_1  |   File "/usr/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 186, in is_valid
web_1  |     self._validated_data = self.run_validation(self.initial_data)
web_1  |   File "/usr/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 364, in run_validation
web_1  |     value = self.to_internal_value(data)
web_1  |   File "/usr/local/lib/python2.7/site-packages/rest_framework_mongoengine/serializers.py", line 197, in to_internal_value
web_1  |     for field in self._writable_fields:
web_1  | AttributeError: 'UserSerializer' object has no attribute '_writable_fields'

This seems to be some problem with the DRF-mongoengine version because when I was using 3.3.0, I had an error about no no attribute named "get_field_names". To resolve that, I moved to 3.3.1 which is the lastest version and I started getting this one.

My serializers.py file is:

from mongoengine import *


class User(Document):
    name = StringField(max_length=50, required=True)
    email = EmailField(max_length=254, required=True)
    password = StringField(max_length=100, required=True)
    role = StringField(max_length=16, default='basic', required=True)

    def __unicode__(self):
        return self.name, self.email

what happens to be the problem?

Hi, @farhatnawaz!

I have a feeling that this is related to the fact that in DRF3.3 fields argument for serializer became mandatory, so now your UserSerializer will look something like this:

class UserSerializer(DocumentSerializer):
    class Meta:
        model = User
        fields = '__all__'

Try adding this 'fields' thing =)

If this doesn't help, could you also provide the code of your url routing code, view/viewset and serializer?

Hey @BurkovBA, thanks for your reply. And sorry, I posted the code above, that's from the models.py file. The serializer code is:

from rest_framework_mongoengine import serializers

from api.models import User

class UserSerializer(serializers.DocumentSerializer):
    class Meta:
        model = User
        fields = '__all__'

This is exactly what you're suggesting and the error still existed. But I have solved it now. It appears to be something with the version of DRF-mongoengine. I moved to 2.0.1 and everything works like a charm.

Though, I would be very interested in knowing the actual cause of that error. Thanks

@farhatnawaz
Great! What if you try these versions of packages?

django==1.9
djangorestframework==3.3.3
mongoengine==0.9
pymongo==2.7
django-rest-framework-mongoengine==3.3.1

@BurkovBA I couldn't try your suggested versions. I had already fixed the issue with DRF-mongoengine 2.0.1 and couldn't find time to test out your versions since I was on a tight schedule.