简体   繁体   English

Django管理员限制外键模型

[英]Django admin limit model from foreign key

I have the following model setup: 我有以下模型设置:

The problem is that when I try to pull the object up in the admin page, computer_names links to several hundred thousand rows that aren't relevant and the page never loads. 问题是,当我尝试在管理页面中提取对象时,computer_names链接到数十万行不相关且页面永远不会加载。 How can I filter computer_names to only the user selected objects for the ManyToMany field? 如何将computer_names仅过滤为ManyToMany字段的用户所选对象?

class ScoringException(models.Model):
    class Meta:
        ordering = ['date_modified']
    requester = models.CharField('Requester',max_length=50,null=False,blank=False)
    computer_names = models.ManyToManyField(Computer)
    domain = models.ForeignKey(Domain)
    exception_kpi_types = models.ManyToManyField(ScoringType)
    expiration_date = models.DateField('Expiration Date')
    reason = models.CharField('Reason',max_length=1000,null=False,blank=False)
    approved = models.BooleanField('Approved')
    date_modified = models.DateTimeField('Date Updated',auto_now=True)

You can use raw_id_fields in the admin so that Django doesn't render the hundred thousand rows of data: 您可以在管理员中使用raw_id_fields ,以便Django不会呈现数十万行数据:

@admin.register(ScoringException)
class ScoringExceptionAdmin(admin.ModelAdmin):
    ....
    raw_id_fields = ['computer_names']

With raw_id_fields , Django will display the list of ids for selected m2m objects. 使用raw_id_fields ,Django将显示所选m2m对象的id列表。 A search button is also added to make adding new objects for the m2m relationship easier. 还添加了搜索按钮,以便更轻松地为m2m关系添加新对象。

See the documentation for more information. 有关更多信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM