このように尋ねられる質問がいくつかあることは知っていますが (このようなもの)、どれも私の問題を解決するのに役立ちませんでした。
モデルに 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)