假设我有一个可以由用户填写个人详细信息的表单,例如
class PersonForm(ModelFormSansSuffix):
class Meta:
model=Person
fields=["name", "nationality"]
nationality = forms.ChoiceField(
label="Country",
help_text="What country are you from?",
required=True,
widget=widgets.CountrySelectWidget(),
choices=countries,
)
name = forms.CharField(
label="Please enter your name",
max_length=512,
required=True,
widget=forms.TextInput()
)
然后我就有了一个模型
Person
class Person(models.Model):
name = models.CharField(max_length=100, null=True, blank=False)
country = CountryField(blank_label='(select country)', default=None, null=True, blank=True)
默认情况下,国家/地区将使用 Alpha2 ISO 标准(即“美国”变为“美国”),但我希望它使用 alpha3 标准(“美国”变为“美国”)。有什么办法可以将 CountryField 设置为使用 alpha3 吗?
您可以如下使用:
person = Person(name="John", country="TR")
person.country.alpha3
>>>"TUR"