简体   繁体   English

django 管理表单上的大型 ManyToMany 关系

[英]Large ManyToMany relations on django admin form

So, I have the following models:所以,我有以下模型:

class Band(models.Model):
    name = models.CharField(max_length=50)

class Contract(models.Model):
    band = models.ForeignKey(Band)
    when = models.DateTimeField(auto_now_add=True)
    salary = models.IntegerField()

class Musician(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    instrument = models.CharField(max_length=100)
    bands = models.ManyToManyField(Band, through=Contract)

class Album(models.Model):
    artist = models.ForeignKey(Musician)
    name = models.CharField(max_length=100)
    release_date = models.DateField()
    num_stars = models.IntegerField()

So, I wanted to expose that on the admin page.所以,我想在管理页面上公开它。 So far, so good.到目前为止,一切都很好。

Note that our musicians here keep jumping in and out from bands.请注意,我们这里的音乐家不断跳进跳出乐队。 Some say one of them even had been on over 2 millions bands in his life-time.有人说其中一位甚至在其一生中参与过超过 200 万支乐队。 I don't know, maybe the bands are Whitesnake, Metallica or something.我不知道,也许这些乐队是 Whitesnake、Metallica 之类的。

How should we do that on the Django Admin Page?我们应该如何在 Django 管理页面上执行此操作?

I tried using raw_id_fields and apart the fact I didn't like the effect, it didn't work so well.我尝试使用raw_id_fields ,除了我不喜欢这种效果之外,它的效果并不好。 It took a lot of time to load and it didn't let me add more ids.加载花费了很多时间,而且它没有让我添加更多的 id。 Weird.诡异的。

I've used admin.StackedInline with no luck cause it will try to load every contract in a which, well, it's gonna take only 2 thousand years.我用过admin.StackedInline没有运气,因为它会尝试加载每个合同,好吧,它只需要 2000 年。

When Musician had a direct relation to Band it worked just fine with this library .当 Musician 与 Band 有直接关系时,它与这个库一起工作得很好。 But now that the relation isn't an straight one.但现在这种关系不是直接的关系。 Looks like autocomplete doesn't support it(it was getting slow anyway).看起来自动完成不支持它(无论如何它变得很慢)。

So, with all of this, I ask you lord SO members.因此,对于所有这一切,我请教各位大神 SO 成员。 What's the best way to do this?最好的方法是什么? Is it autocomplete?它是自动完成的吗? Someone must have had to come across this issue!一定有人遇到过这个问题!

Thanks in advance.提前致谢。

To avoid loading every bands in your admin page use autocomplete_fields Django doc .为了避免加载管理页面中的每个带区,请使用autocomplete_fields Django doc
Just use it like that in your admin.py .只需在您的admin.py像这样使用它。

autocomplete_fields = ('bands',)

Then no bands will be pulled from DB to front, but you will be able to select it through a Select2 search field and it will be printed as "tags".然后不会从 DB 拉到前面的条带,但您将能够通过 Select2 搜索字段选择它,并将其打印为“标签”。

I found this solution and hope it will help somebody in the same situation:我找到了这个解决方案并希望它能帮助处于相同情况的人:

I have many to many relations between the Product and Characteristic model. So, in the admin.py I am setting a form for a Product like the following where catch/get all the Characteristics and make the "prefecth_related" for Characteristic, as well the "select_related" could be done there:我在产品和特征 model 之间存在多对多关系。因此,在 admin.py 中,我正在为产品设置一个表单,如下所示,其中捕获/获取所有特征并为特征制作“prefecth_related”,以及“select_related”可以在那里完成:

class ProductAdminForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['characteristics'].queryset = Characteristic.objects.prefetch_related('category').all()

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

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