1

我在 python django 框架中有模型,例如:

class PropertyTaxItem(models.Model):
     property = models.ForeignKey(Property)

class Property(models.Model):
     citizens = models.ManyToManyField(Citizen, null=True, through = 'Ownership',help_text="a property could belong to multiple citizens")

class Ownership(models.Model):
    as_property=models.ForeignKey(Property,null=True,blank=True, related_name='assets')
    owner_citizen=models.ForeignKey(Citizen,null=True,blank=True, related_name="hs")
    date_started = models.DateField(default=datetime.now())
    date_ended = models.DateField(blank=true)

class Citizen(models.Model):
     first_name = models.CharField(max_length = 50, help_text = 'First name')
     last_name = models.CharField(max_length = 50, help_text = 'Last name')

我想从 PropertyTaxItem 获取表格:

items = PropertyTaxItem.objects.filter(i_status='active').select_related('property').prefetch_related('property__citizens')

我可以通过以下方式获得公民:</p>

for i in items:
    citizens=i.property.citizens.all()

但是如何获得每个公民的 Ownership.date_started 和 Ownership.date_ended?

4

1 回答 1

0

在 Django 的官方查询文档中

基于它: citizens.objects.get(ownership__date_started)等等。

于 2013-11-04T04:46:51.650 回答