Django admin,ManyToManyField>外键关系

2024-10-03 11:23:04 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有这些模型(显然省略了一些属性):

class Product(Model):
    name = CharField(max_length=100)

class Variety(Model):
    product = ForeignKey(Product)

class ProductMerge(Model):
    parent_product = ForeignKey(Product)
    products = ManyToManyField(Product, related_name='merge_proposals')

如何在管理面板的内联中显示所有ProductProductMerge变体

我尝试在TabularInline上设置自定义get_queryset

def get_queryset(self, request):
    return Variety.objects.filter(
        product__in=self.instance.parent.child_products.all())

但我有以下例外:

ValueError: 'marketplace.Variety' has no ForeignKey to 'marketplace.ProductMerge'.


Tags: name模型selfgetmodelproductclassparent