I have the following models:
class District(models.Model):
pk_district = models.AutoField(primary=True)
class Block(models.Model):
pk_block = models.AutoField(primary=True)
class Community(models.Model):
pk_community = models.AutoField(primary=True)
class RelateOne(models.Model):
pk_object = models.OneToOneField('District or Block or Community')
name = models.CharField()
class RelateTwo(models.Model):
pk_object = models.OneToOneField('District or Block or Community')
name = models.CharField()
I want the RelateOne
or RelateTwo
model to associate District
or Block
or Community
, and then I can use it like this:
district = District.objects.get(pk=1)
district.relate_one.name
district.relate_two.name
block = Block.objects.get(pk=1)
block.relate_one.name
block.relate_two.name
Block.objects.select_related('relate_one','relate_two')
How should I set up the model correctly?