我有一个模型Charity与Campaign的ManyToManyField关系,如下所示:
class Charity(models.Model):
name = models.CharField(max_length=200)
description = models.TextField(blank=True,null=True)
tag_line = models.CharField(max_length=160, blank=True,null=True)
campaigns = models.ManyToManyField(Campaign, related_name='campaign_charity')
...
ManyToManyField工作正常,campaign.all()返回与慈善机构关联的正确广告系列。当我尝试使用object.charity_set获取对象的反向集时,我收到以下错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Campaign' object has no attribute 'charity_set'
知道为什么吗?谢谢!
答案 0 :(得分:9)
我相信related_name
属性会覆盖默认的model_set
属性。
我会尝试:
object.campaign_charity.all()
如果您想要查看对象的所有属性/方法,请运行dir(object)