次のような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 を取得する方法は?