Opened 19 years ago

Closed 18 years ago

Last modified 18 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: dev
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:

Description

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/base.py" in get_response
  74. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/src/django/svn-trunk/django/contrib/admin/views/decorators.py" in _checklogin
  54. return view_func(request, *args, **kwargs)
File "/usr/local/src/django/svn-trunk/django/views/decorators/cache.py" in _wrapped_view_func
  40. response = view_func(request, *args, **kwargs)
File "/usr/local/src/django/svn-trunk/django/contrib/admin/views/main.py" in add_stage
  299. return render_change_form(model, manipulator, c, add=True)
File "/usr/local/src/django/svn-trunk/django/contrib/admin/views/main.py" 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
        pass

    def __repr__(self):
        return self.name

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):
        return self.name

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:
        pass

Attachments (1)

models.py (1.2 KB ) - added by mhalle@… 19 years ago.

Download all attachments as: .zip

Change History (4)

by mhalle@…, 19 years ago

Attachment: models.py added

comment:1 by Stefano J. Attardi <django@…>, 19 years ago

Cc: django@… added

comment:2 by Gary Wilson <gary.wilson@…>, 18 years ago

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 by James Bennett, 18 years ago

(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