问题:如何在ModelForm中显示自动字段。我需要在哪里生成并显示一个表单。根据输入,我需要读取AutoField的值并在数据库中搜索。
我的表单:
class CheckinPatientMetaForm(ModelForm):
class Meta:
model = customer
exclude = [
'gender',
'maritalstatus',
'occupation',
'bloodgroup'
]
我的模特:
class customer(models.Model):
# Need autoincrement, unique and primary
cstid = models.AutoField(primary_key=True, unique=True)
name = models.CharField(max_length=35)
age=models.IntegerField()
gender_choices = (('male', 'Male'),
('female', 'Female'))
gender = models.CharField(
choices=gender_choices, max_length=10, default='male')
maritalstatus_choices = ( ('married', 'Married'),
('unmarried', 'Unmarried'))
maritalstatus = models.CharField(
choices=maritalstatus_choices, max_length=10, default='Unmarried')
mobile = models.CharField(max_length=15, default='')
alternate = models.CharField(max_length=15, default='')
email = models.CharField(max_length=50, default='', blank=True)
address = models.CharField(max_length=80, default='', blank=True)
city = models.CharField(max_length=25, default='', blank=True)
occupation = models.CharField(max_length=25, default='', blank=True)
bloodgroup_choices = (('apos', 'A+'),
('aneg', 'A-'),
('bpos', 'B+'),
('bneg', 'B-'),
('opos', 'O+'),
('oneg', 'O-'),
('abpos', 'AB+'),
('abneg', 'AB-'),
('unspecified', '-')
)
bloodgroup = models.CharField(choices=bloodgroup_choices, max_length=3, default='-', blank=True)
class Meta:
unique_together = ["name", "mobile", "age"]
def __str__(self):
return self.name
views.py:
def checkin_patient(request):
results = ''
if request.method == 'POST':
form = CheckinPatientMetaForm(request.POST)
print("POST data",request.POST)
else:
form = CheckinPatientMetaForm()
return render(request, 'clinic/checkin.html', {'rnd_num': randomnumber(), 'form': form, 'SearchResults': results})
模板的相关部分:
<form class="" action="/clinic/checkin" method="post">
{% csrf_token %}
<div class="form-row">
<div class="col-md-24 mb-6">
<label for="cstid" id="lbl-cstid">Hospital ID</label>
{{ form.cstid|add_class:"form-control" }}
</div>
</div>
<div class="form-row">
<div class="col-md-24 mb-6">
<label for="Name">Name</label>
{{ form.name|add_class:"form-control" }}
</div>
</div>
<div class="form-row">
<div class="col-md-2 mb-3">
<label for="Age">Age</label>
{{ form.age|add_class:"form-control" }}
</div>
<div class="col-md-4 mb-3">
<label for="Age">Email</label>
{{ form.email|add_class:"form-control" }}
</div>
</div>
<div class="form-row">
<div class="col-md-12 mb-3">
<label for="Mobile" id="lbl-Mobile">Mobile</label>
{{ form.mobile|add_class:"form-control" }}
</div>
mobile
<div class="col-md-12 mb-3">
<label for="Alternate" id="lbl-Alternate">Alternate</label>
{{ form.alternate|add_class:"form-control" }}
</div>
</div>
<div class="form-row">
<div class="col-md-24">
<label for="Address" id="lbl-Address">Address</label>
{{ form.address|add_class:"form-control" }}
</div>
address
</div>
<div class="form-row">
<div class="col-md-24">
<label for="City" id="lbl-City">City</label>
{{ form.city|add_class:"form-control" }}
</div>
</div>
<div>
<input class="btn btn-primary btton-right" value="Search" id="btnCheckinSearch" type="submit">
</div>
</form>
生成的html:
<form class="" action="/clinic/checkin" method="post">
<input type="hidden" name="csrfmiddlewaretoken" value="XYUgBCj11Vj5s65YvCvR3rsZBdEg990kkBstBmBlUce2TOINIVURk0Yw5X5pqCjJ">
<div class="form-row">
<div class="col-md-24 mb-6">
<label for="cstid" id="lbl-cstid">Hospital ID</label>
</div>
</div>
<div class="form-row">
<div class="col-md-24 mb-6">
<label for="Name">Name</label>
<input type="text" name="name" maxlength="35" class="form-control" id="id_name" style="">
</div>
</div>
<div class="form-row">
<div class="col-md-2 mb-3">
<label for="Age">Age</label>
<input type="number" name="age" class="form-control" id="id_age">
</div>
<div class="col-md-4 mb-3">
<label for="Age">Email</label>
<input type="text" name="email" maxlength="50" class="form-control" id="id_email">
</div>
</div>
<div class="form-row">
<div class="col-md-12 mb-3">
<label for="Mobile" id="lbl-Mobile">Mobile</label>
<input type="text" name="mobile" maxlength="15" class="form-control" id="id_mobile">
</div>
mobile
<div class="col-md-12 mb-3">
<label for="Alternate" id="lbl-Alternate">Alternate</label>
<input type="text" name="alternate" maxlength="15" class="form-control" id="id_alternate">
</div>
</div>
<div class="form-row">
<div class="col-md-24">
<label for="Address" id="lbl-Address">Address</label>
<input type="text" name="address" maxlength="80" class="form-control" id="id_address">
</div>
address
</div>
<div class="form-row">
<div class="col-md-24">
<label for="City" id="lbl-City">City</label>
<input type="text" name="city" maxlength="25" class="form-control" id="id_city">
</div>
</div>
<div>
<input class="btn btn-primary btton-right" value="Search" id="btnCheckinSearch" type="submit">
</div>
</form>
如您所见,cstid没有输入字段。在实际模型中,这是一个自动字段。但是在ModelForm中,它必须是一个表单输入,以便我可以搜索患者的病房(这是自动字段)。如何在表单中显示此字段?