我有两个型号。广告系列和展示位置。每个展示位置对象仅与一个广告系列对象相关。我想列出所有与所有展示位置相关的广告系列对象。
活动#1
- 位置#1
- 放置#2
活动#2
- 放置#3
- 放置#4
...
如何使用一个视图和一个模板进行操作?
广告系列模式:
class Campaign(models.Model):
ACTIVE = 1
INACTIVE = 0
STATUS_CHOICES = (
(ACTIVE, 'Active'),
(INACTIVE, 'Inactive'),
)
CPC = 'cpc'
CPM = 'cpm'
CPV = 'cpv'
status = models.IntegerField(default=1, choices=STATUS_CHOICES)
client_name = models.ForeignKey(Client, on_delete=models.CASCADE)
campaign_name = models.CharField(max_length=100)
start_date = models.DateField(default=datetime.date.today)
end_date = models.DateField(default=datetime.date.today)
def __str__(self):
name = str(self.client_name) + " - " + self.campaign_name
return name
展示位置模式:
class Placement(models.Model):
ACTIVE = 1
INACTIVE = 0
STATUS_CHOICES = (
(ACTIVE, 'Active'),
(INACTIVE, 'Inactive'),
)
CPC = 'cpc'
CPM = 'cpm'
CPV = 'cpv'
UNIT_CHOICES = (
(CPC, 'CPC'),
(CPM, 'CPM'),
(CPV, 'CPV'),
)
status = models.IntegerField(default=1, choices=STATUS_CHOICES)
campaign_name = models.ForeignKey(Campaign, on_delete=models.CASCADE)
publisher_name = models.ForeignKey(Publisher, on_delete=models.CASCADE)
start_date = models.DateField(default=datetime.date.today)
end_date = models.DateField(default=datetime.date.today)
unit_type = models.CharField(default='cpc', choices=UNIT_CHOICES, max_length=3)
units = models.IntegerField(default=0)
budget = models.IntegerField(default=0)
budget_spent = models.IntegerField(default=0)
units_delivered = models.IntegerField(default=0)
def __str__(self):
return str(self.publisher_name)
答案 0 :(得分:1)
{% for campaign in campaigns %}
{{ campaign.campaign_name }}
{% for placement in campaign.placement_set.all %}
{{ placement.status }}
{% endfor %}
{% endfor %}
注意,你真的不应该调用你的外键" foo_name&#34 ;;它们指向实际对象,而不是名称字段,因此应将它们称为campaign
,client
和publisher
。