Code

Opened 8 years ago

Closed 7 years ago

#2280 closed defect (worksforme)

Models, that have not obligatory many-to-one relationships and have no related model set, are not listed.

Reported by: Aidas Bendoraitis <bendoraitis@…> Owned by: adrian
Component: contrib.admin Version: master
Severity: critical Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

This error occurs on Django 0.95 ('post-magic-removal').

When model A relates with model B by many-to-one relationship and that relationship is not obligatory (blank=True is set), only these A-classed objects that have related B-classed objects being set, are displayed in the Django contributed administration on the list page.

A-classed objects, that have no not obligatory related B-classed objects set, are not listed in the Django contributed administration on the list page.

Attachments (0)

Change History (4)

comment:1 Changed 8 years ago by anonymous

Please clarify

comment:2 Changed 8 years ago by Aidas Bendoraitis <bendoraitis@…>

OK. Here is a simple example. Let's say we have models Occupation and Person, where one Person can have one Occupation, and one Occupation can be taken by many People (Persons). Our models would look like this in the code:

from django.db import models

class Occupation(models.Model):
    title = models.CharField(maxlength=200)
    class Admin:
        pass

class Person(models.Model):
    occupation = ForeignKey(Occupation, blank=True)
    name = models.CharField(maxlength=200)
    class Admin:
        pass

Then we create Occupations

  • "Programmer"
  • "Manager"
  • "Designer"

and Persons

  • "Mr. Smith" who is a "Manager"
  • "Tom Anderson" who is a "Programmer"
  • "Trinity", who has no Occupation.

Now if we go to http://example.com/admin/myapp/person/, we will get only "Mr. Smith" and "Tom Anderson" listed, because they have their Occupations set. And that is an error I am reporting.

comment:3 Changed 8 years ago by SmileyChris

I think you need to set null=True as well in your ForeignKey

comment:4 Changed 7 years ago by Gary Wilson <gary.wilson@…>

  • Resolution set to worksforme
  • Status changed from new to closed

I have confirmed, without null=True leaving occupation blank raises a database error as it should, and with null=True the Person gets added just fine and shows on the changelist page.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.