I have these models to present a location
from django.db import models
class Country(models.Model):
name = models.CharField(max_length=50)
country_code = models.CharField(max_length=5)
dial_code = models.CharField(max_length=5)
created_at = models.DateTimeField("date post was created", auto_now_add=True)
updated_at = models.DateTimeField("date post was updated", auto_now=True)
class Meta:
verbose_name = "country"
verbose_name_plural = "countries"
db_table = "countries"
ordering = ["name"]
class State(models.Model):
name = models.CharField(max_length=50)
country = models.ForeignKey(Country, on_delete=models.CASCADE, default=None)
created_at = models.DateTimeField("date post was created", auto_now_add=True)
updated_at = models.DateTimeField("date post was updated", auto_now=True)
class Meta:
verbose_name = "state"
verbose_name_plural = "states"
db_table = "states"
ordering = ["name"]
class City(models.Model):
name = models.CharField(max_length=50)
country = models.ForeignKey(Country, on_delete=models.CASCADE, default=None)
state = models.ForeignKey(State, on_delete=models.CASCADE, default=None)
created_at = models.DateTimeField("date post was created", auto_now_add=True)
updated_at = models.DateTimeField("date post was updated", auto_now=True)
class Meta:
verbose_name = "city"
verbose_name_plural = "cities"
db_table = "cities"
ordering = ["name"]
class Location(models.Model):
street = models.CharField(max_length=100)
additional = models.CharField(max_length=100)
zip = models.CharField(max_length=20)
country = models.ForeignKey(Country, on_delete=models.CASCADE, default=None)
state = models.ForeignKey(State, on_delete=models.CASCADE, default=None)
city = models.ForeignKey(City, on_delete=models.CASCADE, default=None)
phone = models.CharField(max_length=15)
created_at = models.DateTimeField(auto_now_add=True, verbose_name="created at")
updated_at = models.DateTimeField(auto_now=True, verbose_name="updated at")
class Meta:
verbose_name = "location"
verbose_name_plural = "locations"
db_table = "locations"
ordering = ["zip"]
I would like to know if I need the foreign key references fore country and state in the location class? Cities have a foreign key to states and states have a foreign key to countries. So if I just have cities in the location class then surely I can get the state and the country from the city not?
Advice is really appreciated