Django-tables2反向查找

时间:2014-01-01 17:08:12

标签: django django-tables2

我有一个表格,显示一系列“潜在客户”,这些表格很好。还有一个称为“Leadupdate”的相关模型,该模型与表中使用的“lead”模型相关。 Leadupdate与外键有很多关系。我想在其中一个更新列中显示各个“潜在客户”的所有相关更新。网上有几个通过外键跟踪前向关系的例子,但还没有找到一个反向的例子。以下是所述关系Accessor forward look up.

的一个示例

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

Models.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"}

2 个答案:

答案 0 :(得分:0)

根据django docs

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

all_leadtables_for_lead = lead_instance.leadtable_set

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

答案 1 :(得分:0)

一个迟到的答案,但这里的Django 1.8.6中的django-tables2 1.1.0(基于Django-Tables2 Issue 156This answer)对我有用。要通过外键关系访问一对多对象,您只需使用访问器中的related_name,然后创建一个render方法来生成写入列单元格的内容。在该方法中,您可以获取所有外部模型对象并在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"}