Django表单中的动态下拉列表

时间:2016-04-25 16:19:20

标签: python django forms drop-down-menu models

我试图在我的Django帐户注册表单中添加动态下拉列表。

forms.py:

class CompanySignupForm(account.forms.SignupForm):
    first_name = forms.CharField(
        label="First Name",
        max_length=30,
        widget=forms.TextInput(),
        required=True)

    last_name = forms.CharField(
        label="Last Name",
        max_length=30,
        widget=forms.TextInput(),
        required=True)

    company = forms.CharField(
        label="Company Name",
        max_length=60,
        widget=forms.TextInput(),
        required=True)

models.py:

class EmployerProfile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)

    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    company = models.CharField(max_length=40)

    def __unicode__(self):
        return self.company

问题是,如果我想要这个领域"公司"要成为一个由大约10,000个条目组成的下拉列表,最好的方法是什么?我应该使用JS和JSON数据吗? jQuery的自动完成?我只想以最有效的方式做到这一点。

1 个答案:

答案 0 :(得分:1)

您确定要获得10,000个条目的下拉菜单吗?是的,在@Override protected void onNewIntent(Intent intent) { if (intent.getAction().equals(Intent.ACTION_SEARCH)) { SpannableString user_query_spannable = (SpannableString) intent.getCharSequenceExtra(SearchManager.USER_QUERY); //if this was a voice search if (user_query_spannable == null) { //Log, send Analytics hit, etc. //Then perform search as normal... performSearch(intent.getStringExtra(SearchManager.QUERY)); } else { //Log, send Analytics hit, etc. //Then perform search as normal performSearch(intent.getStringExtra(SearchManager.QUERY)); } } }

forms.py

https://docs.djangoproject.com/es/1.9/ref/forms/fields/#choicefield

但我会选择使用http://django-autocomplete-light.readthedocs.org/en/master/

的自动填充字段