我知道有几个这样的问题(比如这个),但没有一个可以帮助我解决我的问题。
我想在我的模型中有一个 City 和 Country 字段,City 的选择取决于 Country;但我不想将 City 和 Country 定义为模型类。这是我的代码:
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
就像字段“国家”一样country = CountryField()
,我想知道是否有一种方法可以在不定义class Country(models.Model)
或的情况下完成任务class City(models.Model)