0

So I have a model that is shown in inline form. That model have ManyToManyField.

Imagine that there are several inline-objects that are already created.

The problem is how to show different querysets of available objects in my m2m-field based on original inline-object.

One more time:) I mean that in each inline-object must by m2m-field with different available variants. Variants will of course include all that is actually set for this inline-object + they must include only variants that are not present at the moment anywhere else.

Thanks.

4

1 回答 1

1

问题写得很糟糕,所以很难确定你在找什么,但我最好的猜测是你想将查询集限制为ManyToManyField没有分配给其他任何东西的项目?如果这是正确的:

你也没有发布示例模型,所以我会补一个来说明

class SomeModel(models.Model):
    my_m2m_field = models.ManyToManyField(OtherModel)

而且,这是基于此限制字段的代码:

class SomeModelInlineAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyInlineAdminForm, self).__init__(*args, **kwargs)

        self.fields['my_m2m_field'].queryset = OtherModel.objects.filter(somemodel__isnull=True)

class SomeModelInlineAdmin(admin.TabularInline):
    model = SomeModel
    form = SomeModelInlineAdminForm
于 2011-11-21T20:17:27.357 回答