I have a Django model with a bunch of fields which describe a Work Order. I then have a form created with ModelForm, but only using a handful of the model's field. Here is my form:
class Service_Call_Form(ModelForm):
class Meta:
model = Work_Order
fields = ('dateWOCreated', 'dateWORequired', 'problemDescription',
'resolutionDescription', 'notesToCrew', 'numHours', 'numWorkers')
I have a Create Work Order HTML template that shows all fields, but NOT "resolutionDescription". In contrast, there is a Work Order Completed HTML template that shows only the "resolutionDescription" field (since, all the user needs to do is explain how the problem was resolved, and not edit what the original problem was).
Now, what I don't understand is that when I click the "Save" button in my template form to complete the work order, all the fields that are shown for the Create Work Order template are cleared out to blank, and the problem resolution field remains. Also, all model fields which I set within the view (and, thus, are not editable by the user) remain in tack.
Why would this be happening?
My Views:
def create_service_call(request):
if request.method == 'POST':
form = Service_Call_Form(request.POST)
if form.is_valid():
wo = form.save(commit=False)
if wo.woID == None:
wo.woID = new_wo_id()
wo.dateWOCreated = datetime.date.today()
wo.systemID = System.objects.get(systemID='2')
wo.woType = 'R'
wo.isComplete = 'N'
wo.save()
return HttpResponseRedirect('/work-orders/')
else:
form = Service_Call_Form()
return render_to_response("pages/work_orders/create_service_call.html", {'form': form}, context_instance=RequestContext(request))
def com开发者_如何学编程plete_service_call(request, scwoID):
if request.method == 'POST':
woEdit = Work_Order.objects.get(pk=scwoID)
form = Service_Call_Form(request.POST, instance = woEdit)
if form.is_valid():
form.save()
return HttpResponseRedirect('/work-orders/')
else:
if scwoID == None:
#Really, this should never happen (not have a woID in the url)
return HttpResponseRedirect('/work-orders/', context_instance=RequestContext(request))
else:
wo = Work_Order.objects.get(pk=scwoID)
form = Service_Call_Form(instance=wo)
return render_to_response("pages/work_orders/complete_service_call.html", {'form': form}, context_instance=RequestContext(request))
The Model:
class Work_Order(models.Model):
IS_COMPLETE_CHOICES = (
('Y', 'Yes'),
('N', 'No'),
)
WO_TYPE_CHOICES = (
('P', 'Plumb'),
('I', 'Install'),
('S', 'Show'),
('R', 'Service'),
('B', 'Blow down'),
('U', 'Start up'),
)
woID = models.CharField(max_length = 10, primary_key = True, default = new_wo_id(), verbose_name = 'Work Order ID')
woType = models.CharField(max_length = 2, verbose_name = 'WO Type', default='R', choices = WO_TYPE_CHOICES)
systemID = models.ForeignKey(System, verbose_name = 'System ID')
notesToCrew = models.TextField(blank = True, null = True, verbose_name = 'Notes to Crew')
dateWOCreated = models.DateField(blank = True, null = True, default=datetime.date.today(), verbose_name = 'Date Created')
dateWORequired = models.DateField(blank = True, null = True, verbose_name = 'Date Required')
numDays = models.DecimalField(max_digits = 3, decimal_places = 0, verbose_name = 'Number of Days to Complete', blank = True, null = True)
numHours = models.DecimalField(max_digits = 3, decimal_places = 2, verbose_name = 'Number of Hours to Complete', blank = True, null = True)
isComplete = models.CharField (max_length = 3, default = 'N', verbose_name = 'Is WO Complete?', choices = IS_COMPLETE_CHOICES)
problemDescription = models.TextField(verbose_name = 'Problem Description', blank = True, null = True)
resolutionDescription = models.TextField(verbose_name = 'Resolution Description', blank = True, null = True)
numWorkers = models.DecimalField(max_digits = 3, decimal_places = 0, verbose_name = 'Number of Workers Required', blank = True, null = True)
def __unicode__(self):
return u'%s - %s' % (self.woID, self.systemID.systemAddress)
class Meta:
ordering = ['woID']
You should have two forms, as you have two completely different types of data being inputed. The first can be a ModelForm (as it saves a bunch of fields to a Model):
class Service_Call_Form(ModelForm):
class Meta:
model = Work_Order
fields = ('dateWOCreated', 'dateWORequired', 'problemDescription',
'notesToCrew', 'numHours', 'numWorkers')
and one just containing ResolutionDescription which I'd just leave as a regular form (as it only updates one field in a model). You may also want model id to be a hidden field, if its not present in the URL or makes it to the request.POST otherwise.
class Resolution_Description_Form(forms.Form):
resolution_description = forms.TextField(max_length=100)
If the the template was from Service_Call_Form your view saved correctly; if you have something with resolution_description
, you should just update that field in a function like:
def complete_work_order(request, scwoID):
if request.method == 'POST':
woEdit = Work_Order.objects.get(pk=scwoID)
form = Resolution_Description_Form(request.POST)
if form.is_valid():
woEdit.resolution_description = form.cleaned_data.get('resolution_description')
woEdit.save()
return HttpResponseRedirect('/work-orders/')
...
EDIT:you could also just have just two ModelForm if you wanted. E.g.,
class Resolution_Description_Form(ModelForm):
class Meta:
model = Work_Order
fields = ('resolutionDescription', )
and just use the appropriate ModelForm in the appropriate template and save as you were saving before.
精彩评论