0

我有一个表格,其中显示了呈现良好的“潜在客户”列表。还有一个名为“Leadupdate”的相关模型与表中使用的“lead”模型相关。Leadupdate 与外键之间存在多对一关系。我想在更新列之一中显示各个“潜在客户”的所有相关更新。网上有几个通过外键跟踪正向关系的例子,但还没有找到一个反向的例子。这是所述关系访问器正向查找的一个示例。

编辑:将在Django-tables2模块实例表上进行查找。我不是要求对模型进行反向查找,而是在 Django-tables2 的上下文中进行。

模型.py:

    class lead(models.Model):
        slug = models.SlugField(unique=True,blank=True, null=True)
        first_name = models.CharField(max_length=100)
        last_name = models.CharField(max_length=100, blank=True, null=True)
        business_name = models.CharField(max_length=100,blank=True, null=True)
        email = models.EmailField(max_length=75, blank=True, null=True)
        phone_number = models.CharField(max_length=20, blank=True, null=True)
        address = models.CharField(max_length=150, blank=True, null=True)
        city = models.CharField(max_length=50, blank=True, null=True)
        state = models.CharField(max_length=10, blank=True, null=True) 
        zipcode = models.CharField(max_length=5, blank=True, null=True)
        submission_date = models.DateTimeField(auto_now_add=True, blank=True)
        assigned_to = models.ManyToManyField(Listing,blank=True, null=True, related_name="leads")
        requested_software = models.CharField(max_length=50, blank=True, null=True)
        type_of_business = models.CharField(max_length=30, choices=TYPE_OF_BUSINESS, default='Bar', blank=True, null=True) 
        time_frame = models.CharField(max_length=10, choices=TIME_FRAME, default='1')
        comments = models.TextField(blank=True, null=True)

        def __unicode__(self):
            return self.business_name
        @models.permalink
        def get_absolute_url(self):
            return('listing_detail', (),{'slug' :self.slug,})

        def save(self, *args, **kwargs):
            if not self.slug:
                self.slug = slugify(self.business_name)
                super(lead, self).save(*args, **kwargs)


class Leadupdate(models.Model):

    CONFIDENCE_LEVEL = (
            ('HOT', 'HOT'),
            ('COLD', 'COLD'),
            )

    LEAD_VALUE = (
            ('1K3K', '1K-3K'),
            ('5K10K', '5K-10K'),
            ('10K20K', '10K-20K'),
            ('20K50K', '20K-50K'),
            ('50KUP', '5OK-UP'),
            )

    ESTIMATED_CLOSING = (
            ('1w4w', '1-4 Weeks'),
            ('1m3m', '1-3 Months'),
            ('3m6m', '3-6 Months'),
            ('6m+', '6+ Months'),
            )

    updatedate = models.DateTimeField(auto_now_add=True)
    update = models.TextField(blank=True, null=True)
    updatefrom = models.ForeignKey(Listing, related_name="update_from", blank=True, null=True)
    lead = models.ForeignKey(lead, related_name="related_update",blank=True, null=True)
    lead_confidence_level = models.CharField(max_length=10, choices=CONFIDENCE_LEVEL, default='COLD', blank=True, null=True)
    estimated_lead_value  = models.CharField(max_length=10, choices=LEAD_VALUE, default='1K3K', blank=True, null=True)
    estimated_closing_frame  = models.CharField(max_length=10, choices=ESTIMATED_CLOSING, default='1-4 Weeks', blank=True, null=True)
    def __unicode__(self):
        return u" %s - %s " % (self.update, self.updatedate)

桌子:

class LeadTable(tables.Table):
    business_name = tables.LinkColumn('lead-detail', args=[A('slug')])
    updates = tables.Column(accessor='lead.related_update')
    class Meta:
        model = lead
        fields = ("business_name","first_name", "last_name","number_of_pos","submission_date","updates")
        attrs = {"class":"paleblue"}
4

2 回答 2

1

一个迟到的答案,但这是在 Django 1.8.6 和 django-tables2 1.1.0 中对我有用的方法(基于Django-Tables2 Issue 156This answer)。要通过外键关系访问一对多对象集,您只需在访问器中使用related_name,然后创建一个渲染方法来生成写入列单元格的内容。在该方法中,您可以获取所有外部模型对象并在 for 循环中访问它们的字段。

class LeadTable(tables.Table):
    business_name = tables.LinkColumn('lead-detail', args=[A('slug')])
    updates = tables.Column(accessor='related_update')
    def render_updates(self, value, table):
        updates = ""
        uFirst = True
        updatesList = list(value.all())
        for u in updatesList:
            if not uFirst:
                updates += ", "
            else:
                uFirst = False
            updates += u.update
        return updates
    class Meta:
        model = lead
        fields = ("business_name","first_name", "last_name","number_of_pos","submission_date","updates")
        attrs = {"class":"paleblue"}
于 2016-01-24T06:12:05.857 回答
0

根据django 文档

在您的视图中,您可以通过这种方式访问​​它们(假设lead_instancelead类的实例):

all_leadtables_for_lead = lead_instance.leadtable_set

附注:使用大写的类名称 ( class Lead(models.Model):) 以遵守 python PEP8 指南

于 2014-01-01T19:15:29.113 回答