我有两个具有OneToMany关系的模型
模型:
class Proveedor(models.Model):
id_proveedor = models.AutoField(primary_key = True)
nombre = models.CharField(max_length=150,blank=False,null=False)
codigo_asignado = models.CharField(max_length=50,blank=False,null=False)
class Meta:
verbose_name_plural = "Proveedor"
def __unicode__(self):
return self.nombre
class Codigo_Corto(models.Model):
id_codigo = models.AutoField(primary_key=True)
id_proveedor = models.ForeignKey(Proveedor,db_column='id_proveedor', verbose_name='Proveedor')
codigo = models.CharField(max_length=15)
tarifa = models.IntegerField()
revenue = models.IntegerField()
moneda = models.CharField(max_length=2)
direccion = models.CharField(max_length=2,blank=True,null=True)
我想创建一个模板,其中只有一个选择域小部件来选择提供者(证明者)和一个html,其中用户从模型中键入剩余字段" Codigo_Corto"
形式:
class CodigoCortoForm(forms.ModelForm):
id_proveedor = forms.ModelChoiceField(queryset=Proveedor.objects.all().order_by('nombre'),required=True,label='Proveedor')
codigo = forms.CharField(widget=forms.TextInput(attrs={'style': 'width:80px'}))
tarifa = forms.DecimalField(widget=forms.NumberInput(attrs={'style': 'width:80px'}),required=True)
revenue = forms.DecimalField(widget=forms.NumberInput(attrs={'style': 'width:80px'}),required=True)
moneda = forms.ChoiceField(
choices = (
('$',"$"),('L',"L"),
),
widget = forms.Select(attrs={'style': 'width:50px'})
,required=True )
direccion = forms.ChoiceField(
choices = (
('MT',"MT"),('MO',"MO"),('nn',"Sin Direccion"),
),
widget = forms.Select(attrs={'style': 'width:120px'})
)
def __init__(self,*args,**kwargs):
super(CodigoCortoForm,self).__init__(*args,**kwargs)
self.helper = FormHelper(self)
class Meta:
model = Codigo_Corto
我的观点:
def CCView(request):
if request.method == 'POST':
form = ccformset(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/home/')
else:
ccformset = formset_factory(CodigoCortoForm)
form = ccformset()
return render_to_response("codcorto.html",
{'form':form},
context_instance=RequestContext(request))
这样我就可以为tabe中的每一行获得一个id_proveedor choifield,但我只需要在表格外面有一个id_proveedor modelchoicefield。
或者我需要单独为id_proveedor渲染一个modelchoicefield并将其传递给formset。
任何建议
提前致谢