Dr. Ose Sunday
Dr. Ose Sunday

Reputation: 11

Referencing more than one field on a Django Model

I am trying to reference 2 fields from a particular django model but it keeps giving me error. The models are displayed below, i want to reference 'building_type' and 'location' from property table to client table

class Property(models.Model):
    location = models.CharField(choices=Property_Location, max_length=120)
    building_type= models.CharField(choices=Property_Type, max_length=120)
    initial_quantity= models.IntegerField(null= False, blank= False)
    quantity_in_stock= models.IntegerField( null= False, blank= False) 
    quantity_sold = models.IntegerField( null= False, blank= False) 

    def __str__(self):
        return self.location



class Client(models.Model):
    first_name = models.CharField(max_length=120)
    last_name = models.CharField(max_length=120)
    phone = models.IntegerField()
    email = models.EmailField(max_length=130)
    building_type= models.ForeignKey(Property,null= False, blank= False,  
    on_delete=models.CASCADE) 
    location= models.ForeignKey(Property, null= False, blank= False, on_delete=models.CASCADE)

    def __str__(self):
       return self.first_name + ' '+ self.last_name

Upvotes: 0

Views: 55

Answers (1)

tohid sadeghian
tohid sadeghian

Reputation: 351

Add related_name to both fields

building_type= models.ForeignKey(Property, on_delete=models.CASCADE,related_name="example1")
location= models.ForeignKey(Property, on_delete=models.CASCADE, related_name="example2")

Upvotes: 1

Related Questions