我在Django 1.5中有一个自引用模型,如下所示:
RELATIONSHIP_PARENT = 1
RELATIONSHIP_BLOCKED = 2
RELATIONSHIP_STATUSES = (
(RELATIONSHIP_PARENT, 'Parent'),
(RELATIONSHIP_BLOCKED, 'Blocked'),
)
class Message(models.Model):
content = models.CharField("Content", max_length=160, db_index=True)
relationships = models.ManyToManyField('self',
through='Relationship',
symmetrical=False,
related_name='related_to')
class Relationship(models.Model):
parent_message = models.ForeignKey(Message, related_name='parent_messages')
child_message = models.ForeignKey(Message, related_name='child_messages')
status = models.IntegerField(choices=RELATIONSHIP_STATUSES)
我将Django管理员配置为在查看单个Relationships
面板时将Message
显示为内联,如下所示:
from django.contrib import admin
from demo.models import Message, Relationship
class RelationshipInline(admin.TabularInline):
model = Relationship
extra = 0
fk_name = 'parent_message'
class MessageAdmin(admin.ModelAdmin):
inlines = (RelationshipInline,)
admin.site.register(Message, MessageAdmin)
admin.site.register(Relationship)
我打算在表中存储许多消息(其中有很多父子连接)。每当我通过管理面板查看个人消息时,我都会看到如下内容:
如红色圆圈所示,Django admin收集数据库中的所有消息,并在菜单中将其显示为下拉列表。我已经阅读了几种方法来阻止它,而我发现的最接近的是Representing ManyToMany relation in the Admin Panel但是当我尝试将raw_id_fields = ('parent_message', )
置于RelationshipInline
级之下时,它似乎什么也没做。< / p>
如果有人可以向我推荐链接或资源,或者只是告诉我如何阻止Django显示下拉列表中的每个条目/消息,我将非常感谢帮助。谢谢。
答案 0 :(得分:2)
这应该有效。
class RelationshipFormSet(BaseInlineFormSet):
def get_queryset(self):
if not hasattr(self, '_queryset'):
criteria = {} #Your criteria here
qs = super(RelationshipFormSet, self).get_queryset().filter(**criteria)
self._queryset = qs
return self._queryset
class RelationshipInline(admin.TabularInline):
model = Relationship
extra = 0
fk_name = 'parent_message'
formset = RelationshipFormSet
答案 1 :(得分:1)
ALL identifier
应该进入管理类:
admin.py
http{
...
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
large_client_header_buffers 4 16k;
...
}