how to have "city" fields depending on "country" field in Django models, without creating their tables
Asked Answered
A

3

6

I know there are several questions asked like this (such as this one), but non of them could help me with my problem.

I wanna have a City and a Country field in my models, which the City choices is depended on Country; BUT I do not want to define City and Country as models classes. here is my code :

from django.contrib.auth.models import User
from django.db import models
from django.forms import ChoiceField
from django_countries.fields import CountryField


class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name="UserProfile")
    name = models.CharField(max_length=30, null=False, blank=False)
    picture = models.ImageField(upload_to='userProfiles/', null=False, blank=False)
    date_of_birth = models.DateTimeField(null=False, blank=False)
    country = CountryField()
    # city = ??
    national_code = models.IntegerField(max_length=10, null=False, blank=False)
    email = models.EmailField()

    def __str__(self):
        return '{}'.format(self.user.username)

    def __unicode__(self):
        return self.user.username

just like field "country" which is country = CountryField(), I wonder if there is a way that I could do the mission without defining the class Country(models.Model) or class City(models.Model)

Antitrust answered 1/10, 2016 at 12:25 Comment(2)
you want to have city and country fields in your model but you dont want to define them? :) even from django_countries.fields import CountryField creates field, so how do you want to save data without a field in database?Dudley
@Dudley - no i mean I want to have city and country fields in a model, but not to define a whole table for City or/and Country; because I don't need them and I just wanna have them as a field of my 'UserProfile' modelAntitrust
S
4

In order to do this you can use django-cities.

However, this will not resolve the issue with the input logic - if you need something like filtering the cities after selecting a country in your forms. You could use django-smart-selects for this but I am not sure how easy it is to implement the complex model structure of django-cities.

Sauerkraut answered 1/10, 2016 at 14:8 Comment(2)
@far did you succeed ??Drizzle
This isn't really answering the question though is it? The question asks without defining a Country model and the django-cities does just that.Bedaub
D
1

the only way for it would be to define choices in your model:

class UserProfile(models.Model):
    CITIES = (
        ('ny', 'New York'),
        ('sm', 'Santa Monica')
        # .. etc
    )
    city = models.CharField(max_length=5, choices=CITIES, blank=True)

more in the docs

Dudley answered 1/10, 2016 at 13:37 Comment(0)
F
1

I had the same problem and I used django-cities-light to populate the city and regions/countries and django-smart-selects for filtering the resulting code was like this and worked well in the admin and in forms:

from django.db import models
from cities_light.models import City
from cities_light.models import Region
from smart_selects.db_fields import ChainedForeignKey

class Address(models.Model):
    ....
    state = models.ForeignKey(Region, on_delete=models.CASCADE)
    city = ChainedForeignKey(City, chained_field="state", chained_model_field="region")

Remember to add 'cities_light' and 'smart_selects' to INSTALLED_APPS. Also add 'smart_selects' to urls.

path('chaining/', include('smart_selects.urls')),
Fassold answered 27/4, 2021 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.