我在 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?