Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django Admin exception when saving a multi-table model #96

Closed
relict007 opened this issue Jul 1, 2014 · 7 comments
Closed

Django Admin exception when saving a multi-table model #96

relict007 opened this issue Jul 1, 2014 · 7 comments

Comments

@relict007
Copy link

I am using cacheops in my Django project with multiple apps. Whenever I try to save a multi-table model it gives a FieldError. In my models the hierarchy is like this:
Item -> Issue -> Suggestion

Save operation for Item class works fine but not for Issue or Suggestion. Error comes only when saving through Admin interface and not through API or django shell. I am using Django 1.6.2 with django-cacheops version 2 installed using pip. Please let know if any more information is needed.

class Item(models.Model):
    user=models.ForeignKey(User,verbose_name='Creator',on_delete=models.PROTECT,related_name='items')
...


Here's class Issue:

class Issue(Item):
...

Here's class Suggestion:

class Suggestion(Issue):
  title=models.CharField('Title',max_length=512)
  description=models.TextField('Description',blank=True)
Environment:


Request Method: POST
Request URL: https://127.0.0.1/admin/uiserver/suggestion/314/

Django Version: 1.6.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'rest_framework',
 'social.apps.django_app.default',
 'apps.secure',
 'rest_framework.authtoken',
 'django_filters',
 'south',
 'django_extensions',
 'djsupervisor',
 'django.contrib.redirects',
 'djcelery',
 'erroneous',
 'debug_toolbar',
 'cacheops',
 'apps.esa',
 'apps.uiserver',
 'apps.device',
 'apps.userprofile',
 'apps.msa',
 'apps.utils')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'django_cprofile_middleware.middleware.ProfilerMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper
  432.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
  198.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func
  25.                 return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in inner
  339.                 return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in change_view
  1231.                 self.save_related(request, form, formsets, True)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in save_related
  882.         form.save_m2m()
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in save_m2m
  96.                 f.save_form_data(instance, cleaned_data[f.name])
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py" in save_form_data
  1527.         setattr(instance, self.attname, data)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py" in __set__
  839.         manager.clear()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py" in clear
  597.             self._clear_items(self.source_field_name)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py" in _clear_items
  723.                     model=self.model, pk_set=None, using=db)
File "/usr/local/lib/python2.7/dist-packages/django/dispatch/dispatcher.py" in send
  185.             response = receiver(signal=self, sender=sender, **named)
File "/usr/local/lib/python2.7/dist-packages/cacheops/query.py" in invalidate_m2m
  529.         objects = sender.objects.filter(**{attname: instance.pk})
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in filter
  163.         return self.get_queryset().filter(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in filter
  590.         return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in _filter_or_exclude
  608.             clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in add_q
  1198.         clause = self._add_q(where_part, used_aliases)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in _add_q
  1234.                     current_negated=current_negated)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in build_filter
  1100.                     allow_explicit_fk=True)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in setup_joins
  1357.             names, opts, allow_many, allow_explicit_fk)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in names_to_path
  1277.                                      "Choices are: %s" % (name, ", ".join(available)))

Exception Type: FieldError at /admin/uiserver/suggestion/314/
Exception Value: Cannot resolve keyword 'suggestion' into field. Choices are: id, image, item


@ttyS15
Copy link
Contributor

ttyS15 commented Jul 1, 2014

Hi!

See at this patch. It rejected, but working in my production.

#94

@Suor
Copy link
Owner

Suor commented Jul 3, 2014

First, sorry for slow response, I'm on vacation right now. Second, multi-table inheritance is not supported right now. README states that it's waiting champion to implement it. I suggest you guys team up on this if you are interested enough.

Regarding #94, I closed it cause it doesn't correspond to stated goal (more robust m2m invalidation) nor does it enables multi-table inheritance support. I will reopen it as solving "Multi-table inheritance exploding" problem. However, it should be complemented with with real support for multi-table models, as silently leaving something hanging up not invalidated would cause grief, see #31.

@4randd, you can try if #94 patch solves your problem. Please come back with a feedback.

@relict007
Copy link
Author

Thanks @ttyS15 @Suor . Yes, the patch solves my problem. Saving models from Admin interface doesn't throw an exception any more. As @ttyS15 mentioned that he has been using this in production so I think I am gonna keep it, until I see any problem related to this.

@Suor I understand that multi-table inheritance doesn't work in cacheops but I read your comment somewhere that by 'doesn't work' it just means that cacheops won't invalidate child model when parent model is updated and vice-versa, so I thought that I check with you about my problem.
I would be interested in implementing multi-table inheritance but I am very new to Django and don't yet understand it enough to make changes at that level.

@Suor
Copy link
Owner

Suor commented Jul 4, 2014

@4randd, you are welcome. You don't really need to understand the whole django to contribute to cacheops. There is also a post explaining how cacheops works - http://hackflow.com/blog/2014/03/09/on-orm-cache-invalidation/.

@relict007
Copy link
Author

Thank @Suor for the very informative post. I will try and do some hacking with cacheops/django.

@Suor
Copy link
Owner

Suor commented Jul 13, 2014

Yes, #94 being merged and released in 2.1 closes this. Thanks everyone.

@Suor Suor closed this as completed Jul 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@Suor @relict007 @ttyS15 and others