I have 3 related models in my system. Each user belongs to a particular place. Users can send messages, and comment on messages, kind of like forum threads.
Here are the 3 models:
class Place(models.Model):
name = models.CharField(max_length=50, unique=True)
slug = models.SlugField(max_length=50, unique=True)
class Message(models.Model):
creator = models.ForeignKey(User)
title = models.CharField(max_length=40)
content = models.CharField(max_length=3000)
date_created = models.DateTimeField(default=timezone.now)
place = models.ForeignKey(Place)
class Comment(models.Model):
creator = models.ForeignKey(User)
content = models.CharField(max_length=3000)
date_created = models.DateTimeField(default=timezone.now)
message = models.ForeignKey(Message)
I want this structure to be reflected in my AdminModels. So for my PlaceAdmin
I wrote this:
class PlaceAdmin(admin.ModelAdmin):
list_display = ('name', 'slug', 'list_messages')
def list_messages(self, obj):
url = reverse('admin:user_content_message_changelist')
return '<a href="{0}?place__id__exact={1}">List messages</a>'.format(url, obj.id)
list_messages.allow_tags = True
list_messages.short_description = 'Messages'
This works perfectly, each place links to a list of messages filtered by that place. So I did the same for my MessageAdmin
:
class MessageAdmin(admin.ModelAdmin):
list_display = ('title', 'list_comments')
def list_comments(self, obj):
url = reverse('admin:user_content_comment_changelist')
return '<a href="{0}?message__id__exact={1}">List comments</a>'.format(url, obj.id)
list_comments.allow_tags = True
list_comments.short_description = 'Comments'
And I get the following error:
SuspiciousOperation at /admin/user_content/comment/
Filtering by message__id__exact not allowed
I don't understand why one is allowed and the other isn't. Any ideas? I'm using Django 1.5.
我意识到自己犯了一个错误-此处显示的代码经过了简化,而Message
模型实际上是从抽象的Content
模型继承的,所以我需要注释列表的URL为:
<a href="{0}?message__content_ptr__exact={1}">List comments</a>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.