Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#23616 closed Bug (fixed)

'GenericRel' object has no attribute 'get_related_field'

Reported by: ranjur Owned by: Konrad Świat
Component: contrib.contenttypes Version: 1.7
Severity: Release blocker Keywords: GenericRel, django 1.7, admin, get_related_field, GenericRelation
Cc: ranju.five@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by ranjur)

I have a model:
#models.py

class TaggedItem(models.Model):
    tag = models.SlugField()
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

    def __str__(self):              # __unicode__ on Python 2
        return self.tag

# And model with GenericRelation
class Bookmark(models.Model):
    url = models.URLField()
    tags = GenericRelation(TaggedItem)

#admin.py

class BookmarkAdmin(admin.ModelAdmin):
    list_filter = ('tags__tag')

admin.site.register(models.Bookmark, BookmarkAdmin)

It will list all the tag in the filter, but when I try to filter it throws error 'GenericRel' object has no attribute 'get_related_field'

Full stack:

AttributeError at /admin/app/bookmark
'GenericRel' object has no attribute 'get_related_field'
Request Method:	GET
Request URL:	http://127.0.0.1:8000/admin/app/bookmark/?q=&tags__tag__id__exact=1
Django Version:	1.7
Exception Type:	AttributeError
Exception Value:	
'GenericRel' object has no attribute 'get_related_field'
Exception Location:	/home/user/virtual/test/local/lib/python2.7/site-packages/django/contrib/admin/options.py in lookup_allowed, line 416
Python Executable:	/home/user/virtual/test/bin/python
Python Version:	2.7.6
Python Path:	
['/home/user/projects/ptest',
 '/home/user/virtual/test/local/lib/python2.7/site-packages/Pillow-2.5.1-py2.7-linux-x86_64.egg',
 '/home/user/virtual/test/src/cmsplugin-bootstrap-carousel',
 '/home/user/virtual/test/src/cmsplugin-contact',
 '/home/user/virtual/test/src/django-faq',
 '/home/user/virtual/test/src/python-varnish',
 '/home/user/virtual/test/src/cmsplugin-text-ng',
 '/home/user/virtual/test/src/django-balancer',
 '/home/user/virtual/test/src/django-nose',
 '/home/user/virtual/test/lib/python2.7',
 '/home/user/virtual/test/lib/python2.7/plat-x86_64-linux-gnu',
 '/home/user/virtual/test/lib/python2.7/lib-tk',
 '/home/user/virtual/test/lib/python2.7/lib-old',
 '/home/user/virtual/test/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/user/virtual/test/local/lib/python2.7/site-packages',
 '/home/user/virtual/test/local/lib/python2.7/site-packages/PIL',
 '/home/user/virtual/test/local/lib/python2.7/site-packages/newrelic-2.28.0.26',
 '/home/user/projects/y1/test']
Server time:	Wed, 8 Oct 2014 13:28:38 +0800
Traceback Switch to copy-and-paste view

/home/user/virtual/test/local/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
                    response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/home/user/virtual/test/local/lib/python2.7/site-packages/django/contrib/admin/options.py in wrapper
                return self.admin_site.admin_view(view)(*args, **kwargs) ...
▶ Local vars
/home/user/virtual/test/local/lib/python2.7/site-packages/django/utils/decorators.py in _wrapped_view
                    response = view_func(request, *args, **kwargs) ...
▶ Local vars
/home/user/virtual/test/local/lib/python2.7/site-packages/django/views/decorators/cache.py in _wrapped_view_func
        response = view_func(request, *args, **kwargs) ...
▶ Local vars
/home/user/virtual/test/local/lib/python2.7/site-packages/django/contrib/admin/sites.py in inner
            return view(request, *args, **kwargs) ...
▶ Local vars
/home/user/virtual/test/local/lib/python2.7/site-packages/django/utils/decorators.py in _wrapper
            return bound_func(*args, **kwargs) ...
▶ Local vars
/home/user/virtual/test/local/lib/python2.7/site-packages/django/utils/decorators.py in _wrapped_view
                    response = view_func(request, *args, **kwargs) ...
▶ Local vars
/home/user/virtual/test/local/lib/python2.7/site-packages/django/utils/decorators.py in bound_func
                return func.__get__(self, type(self))(*args2, **kwargs2) ...
▶ Local vars
/home/user/virtual/test/local/lib/python2.7/site-packages/django/contrib/admin/options.py in changelist_view
                self.list_max_show_all, self.list_editable, self) ...
▶ Local vars
/home/user/virtual/test/local/lib/python2.7/site-packages/django/contrib/admin/views/main.py in __init__
        self.queryset = self.get_queryset(request) ...
▶ Local vars
/home/user/virtual/test/local/lib/python2.7/site-packages/django/contrib/admin/views/main.py in get_queryset
         filters_use_distinct) = self.get_filters(request) ...
▶ Local vars
/home/user/virtual/test/local/lib/python2.7/site-packages/django/contrib/admin/views/main.py in get_filters
            if not self.model_admin.lookup_allowed(key, value): ...
▶ Local vars
/home/user/virtual/test/local/lib/python2.7/site-packages/django/contrib/admin/options.py in lookup_allowed
                rel_name = field.rel.get_related_field().name ...
▶ Local vars

Change History (10)

comment:1 by ranjur, 10 years ago

Description: modified (diff)

comment:2 by Konrad Świat, 10 years ago

Owner: changed from nobody to Konrad Świat
Status: newassigned
Version: 1.7master

I have reproduced this bug, it affects master as well.

contrib/admin/options.py, line 420

rel_name = field.rel.get_related_field().name

getattr() helps to pass it:

rel_name = getattr(field.rel, 'get_related_name', model._meta.pk.name)
Last edited 10 years ago by Konrad Świat (previous) (diff)

comment:3 by Tim Graham, 10 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Version: master1.7

This likely qualifies for a backport since it's a crashing issue. Would be helpful to know if it worked in 1.6.

comment:4 by Konrad Świat, 10 years ago

Has patch: set

comment:5 by Konrad Świat, 10 years ago

In 1.6 there is an AttributeError on related_query_name() instead.

'RelatedObject' object has no attribute 'related_query_name'

Last edited 10 years ago by Konrad Świat (previous) (diff)

comment:6 by Konrad Świat, 10 years ago

1.6 again - workaround on 'related_query_name' shows, that this version is not affected. "if hasattr" condition is not passed and bug line is not executed.

contrib/admin/options.py, line 310

if hasattr(field, 'rel'):
    if field.rel is None:
        # This property or relation doesn't exist, but it's allowed
        # since it's ignored in ChangeList.get_filters().
        return True
    model = field.rel.to
    rel_name = field.rel.get_related_field().name

But there is still (1.6) "related_query_name" AttributeError without workaround.

Last edited 10 years ago by Konrad Świat (previous) (diff)

comment:7 by Konrad Świat, 10 years ago

How do we patch 1.6 in this situation, when changes in 1.7 solve the problem ('related_query_name') and introduce another ('get_related_field' - has solution in PR)? Is the standard procedure of backporting sufficient?

Version 0, edited 10 years ago by Konrad Świat (next)

comment:8 by Tim Graham, 10 years ago

I don't think 1.6 will be patched since that branch is only receiving security fixes at this time per our supported versions policy.

comment:9 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 06b11b617e7d3439bb36b65d1cacd4cc75f36c69:

Fixed #23616 - Fixed generic relations in ModelAdmin.list_filter.

Thanks ranjur for reporting bug, timgraham for review,
and collinanderson for contributing tips.

comment:10 by Tim Graham <timograham@…>, 10 years ago

In fd3dccb8dd4a7ae0ce5e5e5af25452bfca4462a7:

[1.7.x] Fixed #23616 - Fixed generic relations in ModelAdmin.list_filter.

Thanks ranjur for reporting bug, timgraham for review,
and collinanderson for contributing tips.

Backport of 06b11b617e from master

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