對不起,我的英文不好...的ModelForm有空白字段列表
我有一個模式叫居住:
class Habitation(models.Model):
propr = models.ForeignKey(Client, related_name="proprietaire")
locat = models.ForeignKey(Client, related_name="locataire", null=True, blank=True)
etage = models.CharField(max_length=2, blank=True)
numero = models.CharField(max_length=3, blank=True)
ad1 = models.CharField(max_length=64)
ad2 = models.CharField(max_length=64, blank=True)
cp = models.CharField(max_length=5)
ville = models.CharField(max_length=32)
def get_appareils(self):
return Appareil.objects.filter(habitation=self)
def selflink(self):
if self.id:
return '<a href="/admin/client/habitation/%s" target="_blank">Editer</a>' % str(self.id)
else:
return 'Indéfini'
selflink.allow_tags = True
def __unicode__(self):
return u'%s - %s %s' % (self.ad1, self.cp, self.ville)
在他編輯觀點:
def edit(request, habitation_id):
habitation = Habitation.objects.get(pk=habitation_id)
if request.POST:
form = HabitationForm(request.POST, instance=habitation)
if form.is_valid():
form.save()
return redirect('clients')
else:
form = HabitationForm(instance=habitation)
print form.fields
return render_to_response('habitations/edit.html', {
'habitation_id': habitation_id,
'form': form,
}, context_instance=RequestContext(request))
和他模板:
<table>
<form action="/habitations/edit/{{ habitation_id }}/" method="post">
{{ form }}
{% csrf_token %}
{{ form.as_table }}
</form>
</table>
For L:
from django import forms
from client import models
class HabitationForm(forms.ModelForm):
class meta:
model = models.Habitation
fields = ('propr', 'locat', 'etage', 'numero', 'ad1', 'ad2', 'cp', 'ville',)
我的看法(或我的ModelForm)不retrive任何領域,所以沒有更多的表單字段。 有人有什麼建議嗎?
讓我們看看'HabitationForm'定義。 類元: 模型= models.Habitation 字段=( 'propr', 'LOCAT', 'Etage酒店', 'NUMERO' 從django的進口形式 從客戶機進口機型 類HabitationForm(forms.ModelForm) – Rohan
' ,'ad1','ad2','cp','ville',) ' –
更好地更新問題的代碼。 – Rohan