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)
from django_countries.fields import CountryField
creates field, so how do you want to save data without a field in database? – Dudley