简体   繁体   English

Django按州名查询,而不是2个字符的缩写

[英]Django Query by State Name instead of 2 char abbreviation

I have a model that has a MULTISELECTFIELD where the user can select which states relate to them. 我有一个具有MULTISELECTFIELD的模型,用户可以在其中选择与它们相关的状态。 It uses django-localflavor in the backend and so on the form they choose 'TEXAS' and 'TX' is stored on the database. 它在后端使用django-localflavor,依此类推,他们选择了“ TEXAS”和“ TX”的形式存储在数据库中。 I'm using this Q query to find the users that are associated with a state. 我正在使用此Q查询来查找与状态关联的用户。 Q(states__icontains=query) But this only returns a success for 'TX' and not 'TEXAS'. Q(states__icontains=query)但这只会返回“ TX”而不是“ TEXAS”的成功。 That makes sense since that is how it is stored in the database, but how can I search by Texas and get the same return? 这是有道理的,因为那是将其存储在数据库中的方式,但是我如何才能按德克萨斯州进行搜索并获得相同的收益?

All I can think of is to go to the Class Manager ( myclassManager(models.Manager ) and create a get_state_name method that takes in a 2 digit code and returns the state name. If that is the case, I have to make a dictionary with the list by hand. 我所能想到的就是去类管理器( myclassManager(models.Manager )并创建一个get_state_name方法,该方法接受一个2位数的代码并返回状态名称。如果是这种情况,我必须用手工列出。

I feel like the could be a cleaner way. 我觉得这可能是一种更清洁的方式。

Thoughts? 思考?

I just took a look at localflavor and similar to what I suggested in my comment is built in. 我只是看了一下localflavor,它类似于我在评论中建议的内置内容。

from localflavor.us.forms import USStateSelect

class StateSearchForm(forms.Form):
    search = forms.ChoiceField(widget=USStateSelect)

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

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