Django:如何根据另一个应用程序表创建动态下拉列表

时间:2015-10-07 08:22:22

标签: python django django-queryset

我有两个应用程序,App1包含值“Region”(带有重复),在App2中,我想基于不同的“Region”值来显示下拉列表。

App 1 model.py:

class A(models.Model):
    Region=models.CharField(verbose_name='Region',max_length=10,blank=True)
    ......

App 2 model.py:

from django import forms
from django.forms import ModelForm
from django.db import models
from django.contrib import dupont

class input(models.Model):
    Region=models.CharField(max_length=100)

APP 2 Forms.py

from django import forms
from django.forms import ModelForm
from .models import input
from dupont.models import A   /*Here I want to import the class from the 1st App
from django.contrib.auth.models import User
import Queue

class inputform(forms.ModelForm):
    regionlist = forms.ChoiceField(label=u'Region',choices=())
    def __init__(self,*args,**kwargs):
    super(inputform,self).__init__(*args,**kwargs)
    self.fields['regionlist'].choices=((x.que,x.disr) for x in dupont.objects.values('Region').distinct())    

*/*Above code is to make the dynamic drop down list, based on the 1st App's table, and only use the distinct value from the column "Region", but it seems the query is not correct.

此处显示错误:'dict'对象没有属性'que'*

class Meta:
    model = input
    fields = ('Region')

App2 html:

<form action="" method="post">{% csrf_token %}

    <select name="">
        {% for val,name in form.choice_filed.choices %}
            <option value="{{ val }}" {% ifequal data.val val %}selected {% endifequal %}>
        {% endfor %}
    </select>    /* here is to show the drop down list from app2

    <div class="fieldWrapper">
        <p><input type="submit" value="Submit" />p>div>
</form>

目前的错误代码是:'dict'对象没有属性'que'到该下拉列表代码行。

1 个答案:

答案 0 :(得分:0)

dupont.objects.values('Region')会返回list of dictionaries