I have one table with a foreign key to another table? is it possible to populate the foreign key and another field from the same tabe?

I have a table properties_numBed, Each of our properties has a bedroom number assigned to it. What I am trying to achieve is that in the DoorCodes class, We first select the PropertyName(already available from the other table, and then the list of bedrooms attached to that property will populate bedroomnumb(field)

class Properties_NumBed(models.Model):
    PropertyAddress = models.ForeignKey(Properties,on_delete=models.PROTECT)
    NumbBedrooms = models.ForeignKey(Properties_Bedrooms,on_delete=models.PROTECT,default=".") 
    class Meta:
        db_table = "properties_numbBed"

    def __str__(self):
        return str(self.PropertyAddress)

class DoorCodes(models.Model):
    id = models.AutoField(primary_key=True)
    PropertyName = models.ForeignKey(Properties_NumBed, null=True, related_name='PropertyName', on_delete=models.PROTECT)
    bedroomnumb = models.ForeignKey('Properties_Numbed' , on_delete=models.PROTECT, default='0', db_index=False, related_name='+')
    lockcode = models.CharField(max_length=10,null=False, default='0000')
    active = models.BooleanField(default=True)

    class Meta:
        db_table = "lesterdoorcodes"
        ordering = ['PropertyName']

    def __str__(self):
        return str(self.PropertyName)

I have spent many hours trying to google this. Is it not possible to access a foreign key as well as a second or third field?

Sorry if this has been asked and answered multiple times in the past. I have not been able find a solution.

Back to Top