I have a model called Profile. Profile model has a foreign key to a model Location, which in turn has a city field that is a foreign key to a City model which has foreign keys to State and Country
How do I save the Profile so the Location is also saved correctly and the country state and city for the location is correct. The Country, State and City tables are already populated with data so the user will send an id to the back end and the views should then match the id’s with what is in the database.
Do I have to take out the parts for Location from the validated_data object and then save the location separately or how do I do it please?
Location model:
class Location(models.Model):
name = models.CharField(max_length=50, default=None)
slug = AutoSlugField(populate_from=["name"])
street = models.CharField(max_length=100)
additional = models.CharField(max_length=100)
zip = models.CharField(max_length=20)
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"]
def __str__(self):
return self.name
def get_absolute_url(self):
return self.slug
City model:
class City(models.Model):
name = models.CharField(max_length=50)
slug = AutoSlugField(populate_from=["name"])
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"
unique_together = ["country", "state", "name"]
ordering = ["name"]
def __str__(self):
return self.name
def get_absolute_url(self):
return self.slug
The profile model:
class Profile(models.Model):
# Gender
M = 'M'
F = 'F'
GENDER = [
(M, "male"),
(F, "female"),
]
# Basic information
background = models.FileField(upload_to=background_to)
photo = models.FileField(upload_to=image_to)
slug = AutoSlugField(populate_from=['first_name', 'last_name', 'gender', 'location'])
first_name = models.CharField(max_length=100)
middle_name = models.CharField(max_length=100, null=True, blank=True)
last_name = models.CharField(max_length=100)
birthdate = models.DateField()
gender = models.CharField(max_length=1, choices=GENDER, default=None)
bio = models.TextField(max_length=5000)
languages = ArrayField(models.CharField(max_length=30), null=True, blank=True)
# Location information
location = models.ForeignKey(Location, on_delete=models.DO_NOTHING)
website = models.URLField(max_length=256)
# user information
user = models.ForeignKey(User, on_delete=models.CASCADE)
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 = "profile"
verbose_name_plural = "profiles"
db_table = "user_profiles"
def __str__(self):
return self.first_name + ' ' + self.last_name
def get_absolute_url(self):
return self.slug
Any advice will be greatly appreciated. Just want to know if my approach is correct and how I get the data for the location model I must save and if i need to remove the location data from validated data first or what is the best approach. I am using Django Rest Framework