简体   繁体   English

django-localflavor字段未显示在Django管理模型中?

[英]django-localflavor fields not showing up in Django admin models?

I am trying to implement django-localflavors into my Django app. 我正在尝试将django-localflavors实现到我的Django应用程序中。

I import USStateSelect & USZipCodeField at the beginning of my models.py and then include them as a field in my model along with other fields, like so: 我在models.py的开头导入USStateSelect&USZipCodeField,然后将它们与其他字段一起作为模型中的字段包括在内,如下所示:

from localflavor.us.forms import USStateSelect, USZipCodeField
...
Class MyModel(models.Model):
... 
     state = USStateSelect()
     zip_5 = USZipCodeField()

However, when I go to Django admin and try to create a new Model object, I see every other field I wrote (CharFields, etc.) EXCEPT any of the localflavor fields. 但是,当我去Django管理员并尝试创建一个新的Model对象时,我看到我编写的所有其他字段(CharFields等),除了任何localflavor字段。 They are simply not showing up at all as an input field in my Model object form. 它们根本没有显示为我的Model对象形式中的输入字段。 I have done migrations on my database so that is not the issue. 我已经在数据库上进行了迁移,所以这不是问题。

Am I misunderstanding how to use django-localflavor? 我误会了如何使用django-localflavor吗? I read in an answer to a different post that localflavor doesn't actually create input fields, only stores data... but then I've also read that it DOES let you input data. 我在另一篇文章的答案中读到,localflavor实际上并不创建输入字段,仅存储数据...但是我还读到它确实可以让您输入数据。 At this point I am confused. 在这一点上,我感到困惑。 Any help would be appreciated! 任何帮助,将不胜感激!

I think what you are looking for are the model fields . 我认为您正在寻找的是模型领域 The form fields are used when building your own forms (usually outside the admin, such as a contact form). 在构建自己的表单时(通常在管理员外部,例如联系表单),将使用表单字段。 Localflavor has a couple fields that should do what you need. Localflavor有几个应满足您需求的字段。 Note that these are essentially CharField s that have some extra validation to make sure the follow the desired format. 请注意,这些本质上是CharField ,具有一些额外的验证以确保遵循所需的格式。

You need to specify choices option. 您需要指定选择选项。

Change your code a little as below: 如下更改您的代码:

from localflavor.us.forms import USStateSelect, USZipCodeField
...
Class MyModel(models.Model):
... 
     state = USStateSelect(choices=STATE_CHOICES) # add choices
     zip_5 = USZipCodeField() # no change on this line

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM