umutbozkurt / django-rest-framework-mongoengine

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

type object 'XXX' has no attribute '_fields' #277

Open jabbawockeez opened 4 years ago

jabbawockeez commented 4 years ago

I'm using DocumentSerializer to do the job, and I got this message when I was trying to serialize a mongo model object:

/usr/local/lib/python3.6/site-packages/rest_framework_mongoengine/serializers.py in __repr__(self)
    797
    798     def __repr__(self):
--> 799         return serializer_repr(self, indent=1)
    800
    801

/usr/local/lib/python3.6/site-packages/rest_framework_mongoengine/repr.py in serializer_repr(serializer, indent, force_many)
    105         fields = force_many.fields
    106     else:
--> 107         fields = serializer.fields
    108
    109     for field_name, field in fields.items()

/usr/local/lib64/python3.6/site-packages/django/utils/functional.py in __get__(self, instance, cls)
     46         if instance is None:
     47             return self
---> 48         res = instance.__dict__[self.name] = self.func(instance)
     49         return res
     50

/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py in fields(self)
    361         # even if Django's app-loading stage has not yet run.
    362         fields = BindingDict(self)
--> 363         for key, value in self.get_fields().items():
    364             fields[key] = value
    365         return fields

/usr/local/lib/python3.6/site-packages/rest_framework_mongoengine/serializers.py in get_fields(self)
    324
    325         # Retrieve metadata about fields & relationships on the model class.
--> 326         self.field_info = get_field_info(model)
    327         field_names = self.get_field_names(declared_fields, self.field_info)
    328         # Determine any extra field arguments and hidden fields that

/usr/local/lib/python3.6/site-packages/rest_framework_mongoengine/utils.py in get_field_info(model)
     64         pk = None
     65     else:
---> 66         pk = model._fields[model._meta['id_field']]
     67
     68     # Deal with regular fields.

AttributeError: type object 'XXX' has no attribute '_fields'

then I ran the django shell and realized the model has no "_fields" attribute, how does this even work? And by the way, my model is inherit from djongo's model

uoxiu commented 4 years ago

Can you send us the contents of the pip freeze?

jabbawockeez commented 4 years ago

Can you send us the contents of the pip freeze?

amqp==2.5.2 asgiref==3.2.7 backcall==0.1.0 billiard==3.6.3.0 bson==0.5.8 celery==4.4.2 certifi==2020.4.5.1 chardet==3.0.4 coreapi==2.3.3 coreschema==0.0.4 dataclasses==0.7 decorator==4.4.2 Django==3.0.6 django-cors-headers==3.3.0 django-debug-toolbar==2.2 django-filter==2.2.0 django-mongodb-engine==0.6.0 django-rest-framework-mongoengine==3.4.1 djangorestframework==3.11.0 djangotoolbox==1.8.0 djongo==1.3.2 drf-generator==0.0.5 drf-generators==0.5.0 drf-yasg==1.17.1 flake8==3.8.3 idna==2.9 importlib-metadata==1.6.1 inflection==0.4.0 ipython==7.14.0 ipython-genutils==0.2.0 itypes==1.2.0 jedi==0.17.0 Jinja2==2.11.2 kombu==4.6.8 MarkupSafe==1.1.1 mccabe==0.6.1 mongoengine==0.20.0 openapi-codec==1.3.2 packaging==20.4 parso==0.7.0 pexpect==4.8.0 pickleshare==0.7.5 prompt-toolkit==3.0.5 ptyprocess==0.6.0 pycodestyle==2.6.0 pyflakes==2.2.0 Pygments==2.6.1 pymongo==3.10.1 PyMySQL==0.9.3 pyparsing==2.4.7 python-dateutil==2.8.1 pytz==2020.1 redis==3.5.0 requests==2.23.0 rest-meets-djongo==0.0.13 ruamel.yaml==0.16.10 ruamel.yaml.clib==0.2.0 simplejson==3.17.0 six==1.15.0 SQLAlchemy==1.3.16 sqlparse==0.2.4 traitlets==4.3.3 uritemplate==3.0.1 urllib3==1.25.9 vine==1.3.0 wcwidth==0.1.9 yapf==0.30.0 zipp==3.1.0