2014-11-23 87 views
0

如何从django表单中选择opcion Ref域时如何获取产品名称。如何使用Django形式填充字段。模型选择域

class InvoiceForm(forms.Form): 
    invoice= forms.CharField(widget = forms.TextInput()) 
    Ref= forms.ModelChoiceField(queryset=Producto.objects.all()) 
    Product = forms.CharField(widget = forms.TextInput()) 
+0

你必须使用JavaScript来做到这一点。 – ruddra 2014-11-23 04:50:48

回答

0

如果我理解正确,你想要的产品的名称是自动填充的基础上,Ref场。但是,在这种情况下,您根本不需要单独的字段。在模板中,ModelChoiceField将使用Producto__str__方法显示选项。所以也许这样的事情会适合你的需求。

#models.py 
class Producto(models.Model): 
    name = models.CharField() 
    ... 
    def __str__(self): 
     return self.name 

#forms.py 
class InvoiceForm(forms.Form): 
    invoice= forms.CharField(widget = forms.TextInput()) 
    product= forms.ModelChoiceField(queryset=Producto.objects.all()) 

#views.py 
class MyFormView(views.FormView): 
    def form_valid(self, form): 
     product = form.cleaned_data['product'] 
     # can access any product attributes 
     if product.name == 'bananas': 
      # do_something(form) 

保持你的字段名小写(productProduct),这是最好的做法。

相关问题