Skip to content

Instantly share code, notes, and snippets.

@squiddy
Last active Aug 29, 2015
Embed
What would you like to do?
Django 1.5.9 - DisallowedModelAdminToField for M2N Through Object
from django.contrib import admin
from barbaz.models import A, Through
class ThroughInline(admin.TabularInline):
model = Through
fk_name = 'a1'
raw_id_fields = ['a2']
class AAdmin(admin.ModelAdmin):
inlines = [ThroughInline]
admin.site.register(A, AAdmin)
The exception happens when clicking on the raw_id_fields magnifying glass in the admin, the popup will show the exception.
from django.db import models
class A(models.Model):
bs = models.ManyToManyField('self', symmetrical=False, through='Through')
class Through(models.Model):
a1 = models.ForeignKey(A, related_name='a1_set')
a2 = models.ForeignKey(A, related_name='a2_set')
Traceback:
File "/tmp/env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
113. response = callback(request, *callback_args, **callback_kwargs)
File "/tmp/env/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
390. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/tmp/env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/tmp/env/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/tmp/env/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
202. return view(request, *args, **kwargs)
File "/tmp/env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "/tmp/env/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/tmp/env/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "/tmp/env/lib/python2.7/site-packages/django/contrib/admin/options.py" in changelist_view
1198. self)
File "/tmp/env/lib/python2.7/site-packages/django/contrib/admin/views/main.py" in __init__
64. raise DisallowedModelAdminToField("The field %s cannot be referenced." % to_field)
Exception Type: DisallowedModelAdminToField at /admin/barbaz/a/
Exception Value: The field id cannot be referenced.
@smart-alek
Copy link

smart-alek commented Jan 31, 2015

Hey Squiddy:

I work for Sosh and we're still on Django 1.5.9. I'm getting this bug right now and was wondering if you found a workaround for it, or if we're just SOL until we upgrade Django.

Thanks,
Alek Sharma

@smart-alek
Copy link

smart-alek commented Jan 31, 2015

Never mind; you guys fixed it in 1.5.12, so we just upgraded to that instead. SCIENCE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment