I'm working on a real estate app, where I have models for properties, cities and places e.g.
class City(models.Model):
city = models.CharField(max_length=200)
def __str__(self):
return self.city
class Meta:
verbose_name_plural = "Cities"
class Location(models.Model):
address = models.CharField(max_length=200)
city = models.ForeignKey(City)
def __str__(self):
return self.address
class Property(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=200, verbose_name=_('Title'))
slug = models.SlugField(max_length=250, unique_for_date='publish',verbose_name=_('Slug'))
city = models.ForeignKey(City)
address = models.ForeignKey(Location)
status = models.CharField(
max_length=10,
choices=STATUS_CHOICES,
default='draft'
)
This is how it looks in the admin
The problem is both these fields (city & places) are independent, i.e. you can choose London in the city and select a location from a different city. I'm fairly new to django models (or databases), but do you think I should change the address
to just a CharField
in the Property class? How does that relate to the city
?
Ideally I want the autocomplete feature of the 'Google Places API' in both 'City' & 'Location' fields so that user can select both from the API results and then save the result in the database (when Agents will be adding properties) or do a db search using the selected city/location.
I have tried django-google-places but not sure how to incorporate that into a model field just like I want. Thanks in advance