我有两个模型,mpa和Cid-(仅显示相关数据)
class Mpa(models.Model):
mpa_number = models.CharField(max_length=16)
legacy_mpa = models.CharField(max_length=100, null=True, blank=True)
rbc_transit_number = models.CharField(max_length=100, null=True, blank=True)
class Cid(models.Model):
mpa = models.ForeignKey(Mpa,on_delete=models.CASCADE)
anchor_cid = models.CharField(max_length=100, null=False, blank=False)
campaign_name = models.CharField(max_length=100, null=False)
google_cid = models.CharField(max_length=100, null=True, blank=True)
我使用选择相关的在外键上连接了两个表:
result = Cid.objects.all().select_related('mpa')
mpa_数与锚cid有一对多关系。 我想在一行中显示所有锚定cid,它对应于一个mpa_编号(模板中有一个表)。 对于连接锚定cid,我使用了Concat如下-
query_set = result.values('mpa_id').annotate(name = Concat('anchor_cid'))
因为.values()
返回字典.all()
返回模型实例。我想不出一种方法将它们连接在一起并在模板中显示它们。
我已尝试搜索解决方案,但找不到此方案。
也许我正试图用错误的方式来做这件事,有人可以给我指出正确的方向。。
(使用Django 2.1.5和mysql数据库)
在
models.py
或views.py
中执行逻辑可能更容易。例如:这里,} 仅检索实际的
cid_set
是默认的related_name,它允许Mpa
实例定位所有相关的Cid
实例^{cid_anchor
值,作为一个简单列表在模板中,您应该能够执行以下操作:
相关问题 更多 >
编程相关推荐