Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#24852 closed Bug (needsinfo)

Related Field got invalid lookup: offer_ptr

Reported by: Sergey Gornostaev Owned by: nobody
Component: Database layer (models, ORM) Version: 1.7
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have this models in my project:

class Category(MPTTModel):
    parent = TreeForeignKey('self', verbose_name=_("Parent"), null=True, blank=True, related_name='children')
    name = models.CharField(_("Name"), max_length=50)


class Offer(models.Model):
    category = TreeForeignKey(Category, verbose_name=_("Category"))
    title = models.CharField(_("Name"), max_length=100)


class Goods(Offer):
    recommended_goods = models.ManyToManyField('self', verbose_name=_("Recommended goods"), related_name='+', blank=True, null=True)
    recommended_categories = TreeManyToManyField(Category, verbose_name=_("Recommended categories"), related_name='+', blank=True, null=True)


class Promotion(models.Model):
    title = models.CharField(_('Title'), max_length=50)
    description = models.TextField(_("Description"))
    active_from = models.DateTimeField(_("Active from date"))
    active_till = models.DateTimeField(_("Active till date"))
    promotional_categories = models.ManyToManyField(Category, verbose_name=_("Categories"), related_name='+', blank=True, null=True)
    promotional_items = models.ManyToManyField(Offer, verbose_name=_("Goods"), related_name='+', blank=True, null=True)

When I try to execute goods.recommended_categories.all(), it causes an error "Related Field got invalid lookup: offer_ptr"

It began after the field promotional_categories has been added to the Promotion model. But how that can affect on querying Categories from m2m relation in Goods?

Traceback:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/shop/offer/23000/

Django Version: 1.7.8
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'suit',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.sitemaps',
 'django.contrib.humanize',
 'django.contrib.flatpages',
 'django.contrib.redirects',
 'reversion',
 'djangosphinx',
 'registration',
 'mptt',
 'django_mptt_admin',
 'compressor',
 'django_geoip',
 'django_select2',
 'accounts',
 'newslines',
 'vacancies',
 'faq',
 'articles',
 'storage',
 'gallery',
 'banners',
 'portfolio',
 'polls',
 'shop.catalog',
 'shop.basket',
 'shop.reviews',
 'promotions',
 'synchronizer',
 'comments',
 'debug_toolbar')
Installed Middleware:
('debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.middleware.cache.UpdateCacheMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.sites.middleware.CurrentSiteMiddleware',
 'core.middleware.MaintenanceMiddleware',
 'core.middleware.ProtectAdminSiteMiddleware',
 'django.contrib.admindocs.middleware.XViewMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.cache.FetchFromCacheMiddleware')


Traceback:
File "C:\Python\lib\site-packages\django\core\handlers\base.py" in get_response
  104.                     response = middleware_method(request, callback, callback_args, callback_kwargs)
File "C:\Python\lib\site-packages\debug_toolbar\middleware.py" in process_view
  77.             response = panel.process_view(request, view_func, view_args, view_kwargs)
File "C:\Python\lib\site-packages\debug_toolbar\panels\profiling.py" in process_view
  134.         return self.profiler.runcall(view_func, *args, **view_kwargs)
File "C:\Python\lib\cProfile.py" in runcall
  149.             return func(*args, **kw)
File "C:\Python\lib\site-packages\django\views\decorators\http.py" in inner
  41.             return func(request, *args, **kwargs)
File "Z:\Projects\terminal-dev\shop\catalog\views.py" in show_offer
  484.                     for recommendation in goods.recommended_categories.all():
File "C:\Python\lib\site-packages\django\db\models\manager.py" in all
  191.         return self.get_queryset()
File "C:\Python\lib\site-packages\django\db\models\fields\related.py" in get_queryset
  885.                 return qs._next_is_sticky().filter(**self.core_filters)
File "C:\Python\lib\site-packages\django\db\models\query.py" in filter
  691.         return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  709.             clone.query.add_q(Q(*args, **kwargs))
File "C:\Python\lib\site-packages\django\db\models\sql\query.py" in add_q
  1331.         clause, require_inner = self._add_q(where_part, self.used_aliases)
File "C:\Python\lib\site-packages\django\db\models\sql\query.py" in _add_q
  1358.                     current_negated=current_negated, connector=connector)
File "C:\Python\lib\site-packages\django\db\models\sql\query.py" in build_filter
  1225.                                                     lookups, value)
File "C:\Python\lib\site-packages\django\db\models\fields\related.py" in get_lookup_constraint
  1577.             raise TypeError('Related Field got invalid lookup: %s' % lookup_type)

Exception Type: TypeError at /shop/offer/23000/
Exception Value: Related Field got invalid lookup: offer_ptr

I was confused by this lines displayed in traceback interactive view:

C:\Python\lib\site-packages\django\db\models\sql\query.py in _add_q:
1356.                child_clause, needed_inner = self.build_filter(
1357.                    child, can_reuse=used_aliases, branch_negated=branch_negated,
1358.                    current_negated=current_negated, connector=connector) 

Local vars:
used_aliases    set([u'catalog_category',
                   u'promotions_promotion',
                   u'promotions_promotion_promotional_categories'])
child           (u'+__offer_ptr', 23000)

What's used_aliases?

Change History (3)

comment:1 by Baptiste Mispelon, 9 years ago

I wonder if this could be the same bug as #24505.

What happens if you change all the related_name='+' to 1+, 2+, 3+, ... (so that there aren't two identical related_name)?

Thanks.

comment:2 by Tim Graham, 9 years ago

Resolution: needsinfo
Status: newclosed

Please reopen if you can reproduce on master after the fix Baptiste noted and also without django-mptt. Thanks!

in reply to:  1 comment:3 by Sergey Gornostaev, 9 years ago

Replying to bmispelon:

I wonder if this could be the same bug as #24505.

What happens if you change all the related_name='+' to 1+, 2+, 3+, ... (so that there aren't two identical related_name)?

Thanks.

Yes, it helped! It is necessary to write about this in the documentation.

Note: See TracTickets for help on using tickets.
Back to Top