Django Admin外键

时间:2013-08-07 08:04:13

标签: django python-2.7 django-admin django-admin-filters

我有两张桌子: -

class Package(models.Model):

    '''Model to represent details of the packae booking'''
    date_of_inquiry = models.DateField(blank=True, null=True)
    agent_name = models.CharField(max_length=200, blank=True)
    type_of_booking = models.ForeignKey(TypeOfBooking, blank=True, null=True)
    no_of_pax = models.IntegerField(null=True)
    source_of_inquiry = models.CharField(max_length=100, blank=True)
    business_vendor = models.CharField(max_length=100, blank=True)
    travel_date = models.DateField(null=True, blank=True)
    reply_date = models.DateField(null=True, blank=True)
    client_name = models.CharField(max_length=500, blank=True)
    client_email = models.EmailField(blank=True)
    client_contacts = models.CharField(max_length=200, blank=True)
    inquiry_assigned_to = models.ForeignKey(User, blank=True, null=True)

    def __unicode__(self):
        return str(self.date_of_inquiry) + "  " + self.client_name


class FollowUp(models.Model):

    '''Model to represent follow-ups of the clients'''
    follow_up_date = models.DateField(blank=True, null=True)
    remarks = models.TextField(max_length=500, blank=True)
    package = models.ForeignKey(Package, blank=True, null=True)
    status_inquiry = models.ForeignKey(StatusInquiry, blank=True, null=True)
    followup_done_by = models.ForeignKey(User, blank=True, null=True)

    def __unicode__(self):
        return str(self.follow_up_date) + " " + self.status_inquiry.status_name

我想在列表视图的管理面板中访问包,其中显示以下属性: -

list_display('agent_name','date_of_inquiry',##########) 注意:##############字段应该是最近后续同一包的状态。

如何实现?非常感谢你的回答。

1 个答案:

答案 0 :(得分:1)

您可以在类Package中的模型中定义一个函数。由于您需要知道状态,您可以使用

查找最近的后续状态
def get_status_name(self):
    a = self.followup_set.latest('follow_up_date')
    return a.status.status_name