Opened 14 years ago

Closed 13 years ago

Last modified 13 years ago

#1808 closed defect (worksforme)

ForeignKey fields produce errors when subobjects do not have Admin interfaces themselves

Reported by: mhalle@… Owned by: Adrian Holovaty
Component: contrib.admin Version: master
Severity: normal Keywords:
Cc: django@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


If a model contains other models using ForeignKey fields, and the submodels don't have have their own explicit Admin interface (ie, have "class Admin" in their class definitions), then creating those subobjects will produce an the following kind of error:

Traceback (most recent call last):
File "/usr/local/src/django/svn-trunk/django/core/handlers/" in get_response
  74. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/src/django/svn-trunk/django/contrib/admin/views/" in _checklogin
  54. return view_func(request, *args, **kwargs)
File "/usr/local/src/django/svn-trunk/django/views/decorators/" in _wrapped_view_func
  40. response = view_func(request, *args, **kwargs)
File "/usr/local/src/django/svn-trunk/django/contrib/admin/views/" in add_stage
  299. return render_change_form(model, manipulator, c, add=True)
File "/usr/local/src/django/svn-trunk/django/contrib/admin/views/" in render_change_form
  196. field_sets = opts.admin.get_field_sets(opts)

  AttributeError at /admin/polls/license/add/
  'NoneType' object has no attribute 'get_field_sets'

This error was generated for the following model. In this case, adding an "Institution" will work, but adding a "License" will produce the above error. Also, there's no way to add an "Operating System" for the same reason. All work correctly if the subobjects are explicitly declared to have Admin interfaces.

from django.db import models

class Institution(models.Model):
    name = models.CharField(maxlength=200, core=True)
    reference_url = models.CharField(maxlength=200, core=True)
    class Admin:   # because of this field, no error here

    def __repr__(self):

class License(models.Model):
    # no Admin class will produce an error when added
    title = models.CharField(maxlength=200)
    reference_url = models.URLField()
    description = models.TextField()
    def __repr__(self):
        return self.title

class OperatingSystem(models.Model):
    # no Admin class: no way to add this object as a subpart of SoftwarePackage
    name = models.CharField(maxlength=200, core=True)
    def __repr__(self):

class SoftwarePackage(models.Model):
    title = models.CharField(maxlength=200)
    institution = models.ForeignKey(Institution, null=True)
    license = models.ForeignKey(License, null=True)
    operating_system = models.ManyToManyField(OperatingSystem, null=True)
    open_source = models.BooleanField()

    def __repr__(self):
        return self.title
    class Admin:

Attachments (1) (1.2 KB) - added by mhalle@… 14 years ago.

Download all attachments as: .zip

Change History (4)

Changed 14 years ago by mhalle@…

Attachment: added

comment:1 Changed 14 years ago by Stefano J. Attardi <django@…>

Cc: django@… added

comment:2 Changed 13 years ago by Gary Wilson <gary.wilson@…>

Resolution: worksforme
Status: newclosed

I have no problem adding objects in the admin interface or in the shell. If no class Admin is specified, then then those type of objects cannot be added through the admin.

comment:3 Changed 13 years ago by James Bennett

(In [4673]) 0.91-bugfixes: Fixed #999 by resolving name clash in the metasystem which could confuse manipulators about which fields they should follow. Refs #1808, #1826, #1839 and #2415, which are variations of this that persist in trunk.

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