django admin中的ForeignKey字段 [英] ForeignKey Field in django admin

查看:351
本文介绍了django admin中的ForeignKey字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户模型中,大约有15万个条目。因此,当我在没有raw_id_fields的django-admin中使用它时,在加载所有条目作为外键选择菜单时会引起问题。
是另一种方法,因此可以轻松加载它或使其可搜索。

There are around 1.5 lakhs entry in User model. So when i am using it in django-admin without the raw_id_fields it is causing problem while loading all the entry as a select menu of foreign key. is there alternate way so that it could be loaded easily or could become searchable.

基本上,我具有上述定义的这些模型,并且有一个User模型在ProfileRecommendation模型中用作ForeignKey。因此,用户模型的数据库条目包含大约1,50,000个条目。我不希望这些外国字段的默认选择选项。相反,if可以过滤掉它们并仅加载用户表的少量条目。或者无论如何,我可以使它们像自动完成建议一样可搜索

Basically i have these models as of defined above and there is a User model which is used as ForeignKey in ProfileRecommendation models. so the database entry for user model consist of around 1,50,000 entries. I don't want default select option for these foreign fields. Instead if can filter them out and load only few entries of the user table. Or anyhow i can make them searchable like autocomplete suggestion

class ProfileRecommendationAdmin(admin.ModelAdmin):
    list_display = ('user', 'recommended_by', 'recommended_text')
    raw_id_fields = ("user", 'recommended_by')
    search_fields = ['user__username', 'recommended_by__username', ]
    admin.site.register(ProfileRecommendation, ProfileRecommendationAdmin)



models.py



models.py

class ProfileRecommendation(models.Model):
    user = models.ForeignKey(User, related_name='recommendations')
    recommended_by = models.ForeignKey(User, related_name='recommended')
    recommended_on = models.DateTimeField(auto_now_add=True, null=True)
    recommended_text = models.TextField(default='')


推荐答案

我建议您使用 django-select2-forms

使用该包,您可以如下定义模型外部字段:

Using that package, you can define your model foreign field as below:

from select2.fields import ForeignKey

class Author(models.Model):
  name = models.CharField(max_length=100)
  active = models.BooleanField()

class Entry(models.Model):
    author = ForeignKey(Author,
    limit_choices_to=models.Q(active=True),
    ajax=True,
    search_field='name',
    overlay="Choose an author...",
    js_options={
        'quiet_millis': 200,
    },
    on_delete=models.CASCADE)

在django admin中,这将使用ajax加载作者的姓名,该名称应比使用默认admin ForeignKey字段快得多。同样从示例中可以看到,您可以使用 limit_choices_to 来仅过滤可供选择的所需值。

In django admin this will load authors' names using ajax, which should work much faster than using default admin ForeignKey field. Also as you can see from example, you can use limit_choices_to for filtering only needed values available for selection.

如果不是django-select2-forms ,您可以使用任何其他可用于django自动完成功能的软件包:此处的自动完成软件包列表

If not django-select2-forms, you can play with any other package available for django autocomplete: list of autocomplete packages here

我从列表中尝试了一些,但是django-select2-forms看起来对我来说是最好的。

I tried few from the list, but django-select2-forms looks like the best for me.

这篇关于django admin中的ForeignKey字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆