Django

Code

Ticket #1808 (closed: worksforme)

Opened 2 years ago

Last modified 1 year ago

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

Reported by: mhalle@bwh.harvard.edu Assigned to: adrian
Component: Admin interface Version: SVN
Keywords: Cc: django@attardi.org
Triage Stage: Unreviewed Has patch: 0
Needs documentation: 0 Needs tests: 0
Patch needs improvement: 0

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

models.py (1.2 kB) - added by mhalle@bwh.harvard.edu on 05/08/06 11:11:01.

Change History

05/08/06 11:11:01 changed by mhalle@bwh.harvard.edu

  • attachment models.py added.

05/15/06 17:36:44 changed by Stefano J. Attardi <django@attardi.org>

  • cc set to django@attardi.org.

02/26/07 23:37:03 changed by Gary Wilson <gary.wilson@gmail.com>

  • status changed from new to closed.
  • resolution set to worksforme.

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.

03/07/07 11:03:44 changed by ubernostrum

(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.


Add/Change #1808 (ForeignKey fields produce errors when subobjects do not have Admin interfaces themselves)




Change Properties
Action