djongo icon indicating copy to clipboard operation
djongo copied to clipboard

Django admin page not working for EmbeddedField field in the documentation example

Open csyan5 opened this issue 5 years ago • 10 comments

I tried to implement the blog example in the Use Django Admin to add documents section on this page

I used the exact same code. When I added a new Entry on the admin page. It showed the error 'NoneType' object has no attribute 'attname'. The entire error traceback is:

Internal Server Error: /admin/publication/entry/add/
Traceback (most recent call last):
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
   response = get_response(request)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
   response = self.process_exception_by_middleware(e, request)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
   response = wrapped_callback(request, *callback_args, **callback_kwargs)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/contrib/admin/options.py", line 607, in wrapper
   return self.admin_site.admin_view(view)(*args, **kwargs)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
   response = view_func(request, *args, **kwargs)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
   response = view_func(request, *args, **kwargs)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 231, in inner
   return view(request, *args, **kwargs)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1638, in add_view
   return self.changeform_view(request, None, form_url, extra_context)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
   return bound_method(*args, **kwargs)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
   response = view_func(request, *args, **kwargs)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1522, in changeform_view
   return self._changeform_view(request, object_id, form_url, extra_context)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1558, in _changeform_view
   form_validated = form.is_valid()
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/forms/forms.py", line 180, in is_valid
   return self.is_bound and not self.errors
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/forms/forms.py", line 175, in errors
   self.full_clean()
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/forms/forms.py", line 378, in full_clean
   self._post_clean()
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/forms/models.py", line 404, in _post_clean
   self.instance.full_clean(exclude=exclude, validate_unique=False)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/db/models/base.py", line 1200, in full_clean
   self.clean_fields(exclude=exclude)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/db/models/base.py", line 1242, in clean_fields
   setattr(self, f.attname, f.clean(raw_value, self))
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 648, in clean
   value = self.to_python(value)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/djongo/models/fields.py", line 242, in to_python
   f'Value: {value} must be an instance of {self.base_type}')
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/db/models/base.py", line 521, in __str__
   return '%s object (%s)' % (self.__class__.__name__, self.pk)
 File "/Users/yan/PycharmProjects/my_project/venv/lib/python3.7/site-packages/django/db/models/base.py", line 568, in _get_pk_val
   return getattr(self, meta.pk.attname)
AttributeError: 'NoneType' object has no attribute 'attname'

package version:

Package         Version
--------------- -------
asgiref         3.2.10
Django          3.0.5
djongo          1.3.3
numpy           1.19.0
pandas          1.0.5
pip             20.1.1
pymongo         3.10.1
PyMySQL         0.9.3
python-dateutil 2.8.1
python-dotenv   0.14.0
pytz            2020.1
setuptools      49.1.0
six             1.15.0
SQLAlchemy      1.3.18
sqlparse        0.2.4

python version: Python 3.7.1 mongodb version: MongoDB 4.2.6 Community

csyan5 avatar Jul 13 '20 22:07 csyan5

I have the same Python 3.6, Django 2 & 3 Djongo 1.3.3 & 1.3.2

Djongo 1.3.1 works

fonar avatar Jul 19 '20 06:07 fonar

Hi @fonar, do you mean Djongo 1.3.1 works, but 1.3.3 & 1,3,2 don't work?

csyan5 avatar Jul 19 '20 06:07 csyan5

Hi @ChangshengYan yes. If you'll find a solution to make it work with 1.3.3, please, let me know. Thanks!

fonar avatar Jul 19 '20 06:07 fonar

Sure. Thanks for your information! @fonar

csyan5 avatar Jul 19 '20 06:07 csyan5

I've encountered the same issues on Djongo 1.3.3 with an existing model that was working on previous versions. (Using Django 2.2.14, if relevant.)

Additionally, attempting to view an existing model also causes an AttributeError, except this time it seems to be expecting an attribute _meta in the dictionary containing the contents of the EmbeddedField. Not sure if this is related, or a separate issue yet.

infinityredux avatar Jul 30 '20 10:07 infinityredux

Hi @infinityredux and @fonar, I had decided to switch from MongoDB to Postgres because Django has better support for Postgres, including full-text search, indexing, and object-relational mapping. Currently, the app works fine with Postgres (~300MB data, full-text search in ~70000 rows), with no functionality trimmed or affected, and the read/write speed seems no affected.

csyan5 avatar Jul 30 '20 14:07 csyan5

Try set __str__ to your model class:

    def __str__(self):
        return f'{self.name}'

brunowego avatar Oct 29 '20 21:10 brunowego

@brunowego Thanks alot, but in my case it helps with djongo 1.3.1, but in djongo 1.3.3 it still drops the same error

Phoeux avatar Dec 17 '20 08:12 Phoeux

Hi @infinityredux and @fonar, I had decided to switch from MongoDB to Postgres because Django has better support for Postgres, including full-text search, indexing, and object-relational mapping. Currently, the app works fine with Postgres (~300MB data, full-text search in ~70000 rows), with no functionality trimmed or affected, and the read/write speed seems no affected.

Thanks for sharing. I am also facing a lot many issue with djongo.

sachinaaglave avatar May 08 '21 11:05 sachinaaglave

I can understand this last comment. I have bee² running into several issues with djongo the last couple of days. I find most of them here in the issues, but all without solution. I'm seriously considering abandoning the idea of using django on mongo.

vindevoy avatar Jun 12 '22 07:06 vindevoy