0

这是我的模型:

class Store_product(models.Model):
    breakdown_lst = models.ManyToManyField('self',symmetrical=False,through='Kit_breakdown_assoc',related_name='kit_lst')

class Kit_breakdown_assoc(models.Model):
    kit = models.ForeignKey(Store_product,related_name='breakdown_assoc_lst')
    breakdown = models.ForeignKey(Store_product,related_name='kit_assoc_lst')
    qty = models.IntegerField()

我想做的是:

sp = Store_product.objects.get(pk=sp_id)
sp.kit_breakdown_assoc_set.all() --> error: 'Store_product' object has no attribute 'kit_breakdown_assoc_set'

Django 文档提到了一个“规则”,即带有“self”的 m2m 不会将 xxx_set 属性添加到类中。我认为我的案例只是这个“规则”的一个特例,因为它有一个中间模型。我发现我无法访问我的 through-related-manager 很不方便。我错过了什么或者可以实现这个功能吗?

4

1 回答 1

0

The problem is that, as you've correctly specified a related name, breakdown_assoc_lst, your intermediate model manager is not accessed by sp.kit_breakdown_assoc_set, but by sp.breakdown_assoc_lst, that is what you specify in related name.

于 2014-10-31T18:27:17.227 回答