0
I have a problem with an imported database for Django 1.6.5. In this database the columns follow the pattern: id_city, name, id_state and etc. However, Django did not handle it well leaving the class like this:
class City(models.Model):
id_city = models.IntegerField(primary_key=True)
state = models.ForeignKey('State', db_column='id_state', blank=False, null=False)
name = models.CharField(max_length=50, blank=False)
class Meta:
managed = False
db_table = 'city'
To access the id_state field of the table through the model I have two alternatives:
- city.state_id
- city.state.id_state
In the first alternative it is confusing to work with a name other than the name in the bank and in the second alternative it is necessary an additional query to know a single field.
I searched the documentation but I couldn’t find a simple way to use the attribute the way it is in the bank, which would be like this:
city.id_state
What you want to understand is the concept of relationship between one table and the other ? relatedly 1 to 1 ,1 to many ? would be what you want ?
– stack.cardoso