Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#7875 closed (worksforme)

Admin changelist not showing any rows when having multiple foreignkeys in model.

Reported by: luddep@… Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords: foreignkeys newforms-admin
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


When using two foreignkeys, such as:

class Comment(models.Model):
	author = models.CharField(max_length=140)
	body = models.TextField()
	post = models.ForeignKey(Post)
	parent = models.ForeignKey('self')

the admin changelist for that model doesn't show any rows:

Rows are only shown when I comment out one of the foreignkeys, doesn't matter which.
This only occurs when using two ForeignKeys in the same model.

I'm running the latest trunk (8031).

Attachments (1)

Picture 1.png (169.5 KB) - added by luddep@… 8 years ago.
screenshot of problem

Download all attachments as: .zip

Change History (11)

Changed 8 years ago by luddep@…

Attachment: Picture 1.png added

screenshot of problem

comment:1 Changed 8 years ago by Brian Rosner

milestone: 1.0 alpha
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

Dont mark a milestone until it is verified. I have several models with multiple foreign keys and they work fine. Can you also share what your ModelAdmin looks like?

comment:2 Changed 8 years ago by anonymous

oh, whops! First time submitter - sorry.

class CommentAdmin(admin.ModelAdmin):
	ordering = ('-date',)
	list_display = ('author', 'body', 'date', 'note',)
	search_fields = ('author','body',)
	list_filter = ('spam','date','note',), CommentAdmin)

comment:3 Changed 8 years ago by Jacob

milestone: 1.0
Triage Stage: UnreviewedAccepted

comment:4 Changed 8 years ago by David Gouldin

Resolution: worksforme
Status: newclosed

I can't reproduce this bug with the model and admin classes provided. Can you provide a minimal example with data to illustrate the issue?

comment:5 Changed 8 years ago by Ludwig Pettersson

Resolution: worksforme
Status: closedreopened

I found a solution a while ago but forgot to check back:

If I add either of the two foreignkeys ( Parent or Note ) to my list_display all the rows disappear from my admin.

Bad - no rows show:

	list_display = ('author','body', 'date','note',)

Good - everything is fine:

	list_display = ('author','body', 'date',)

If this doesn't help I guess it's something with my data - but it would be weird that with fix it would be my data.

comment:6 Changed 8 years ago by Gary Wilson

Resolution: worksforme
Status: reopenedclosed

There is no "note" in the model example you provided, the only foreign keys are "post" and "parent". I was sitting next to David (dgouldin) when he tried with both "post" and "parent" in list_display and could not reproduce the problem. He also tried creating a circular reference with the parent fields, but still the rows displayed as they should.

Are you using the latest trunk revision? Please provide a minimal (and complete) model and list_display example that reproduces your problem. Until then, closing this as worksforme.

comment:7 Changed 8 years ago by Ludwig Pettersson

Ah, it seems that I changed note to post for clarification when I first submitted it.

Using the latest trunk (8423).

Complete models & model admin for comment where the rows are a no show as long as 'note' is in list_display:

class Note(models.Model):
	title = models.CharField(max_length=140)
	url = models.URLField(blank=True)
	slug = models.SlugField()
	body = models.TextField()
	tags = models.ManyToManyField(Tag)
	date = models.DateTimeField()
	published = models.BooleanField(default=True)
	internal = models.BooleanField()
	def __unicode__(self):
		return self.title
	class Meta:
		ordering = ["-date"]

	def get_absolute_url(self):
		return "/notebook/%s/%02d/%02d/%s/" % (,,, self.slug)

	def comment_set_ham(self):
		return self.comment_set.filter(spam=False)

	def comment_set_ham_parents(self):
		return self.comment_set.filter(spam=False).filter(parent=None)

class Comment(models.Model):
	author = models.CharField(max_length=140)
	email = models.EmailField()
	website = models.URLField(blank=True)
	body = models.TextField()
	date = models.DateTimeField()
	spam = models.BooleanField(default=False)
	ip = models.IPAddressField()
	note = models.ForeignKey(Note)
	parent = models.ForeignKey('self')
	def __unicode__(self):
		return self.body
	class Meta:
		ordering = ["date"]
	def comment_set_ham_children(self):
		return self.comment_set.all().filter(spam=False)

Full admin:

class CommentAdmin(admin.ModelAdmin):
	ordering = ('-date',)
	list_display = ('author','body', 'date','note',)
	search_fields = ('author','body',)
	list_filter = ('spam','date','note',)
	fieldsets = (
		(None, {
			'fields': ('author', 'email', 'website', 'body', 'date')
		('Advanced options', {
			'classes': ('collapse',),
			'fields': ('ip','note','parent')

comment:8 Changed 8 years ago by Ludwig Pettersson

Resolution: worksforme
Status: closedreopened

comment:9 Changed 8 years ago by James Bennett

Resolution: worksforme
Status: reopenedclosed

Using almost the model and admin definitions above, I cannot reproduce this. However, I say "almost" because it's impossible to insert data into the Comment model, as defined, since the foreign key to "self" is required but can't be filled in for the very first Comment entered.

comment:10 Changed 5 years ago by Jacob

milestone: 1.0

Milestone 1.0 deleted

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