我有三个这样的模型类:InstallationReportPartInstance
与InstallationReportData
有多对一的关系。
class InstallationReportData(FormData):
installationDate = models.DateField(null=True, blank=False, verbose_name="Date of Installation")
deliveryOrder = models.CharField(null=True, blank=True, max_length=255, verbose_name="Delivery Order")
unitSerialNumber = models.CharField(null=True, blank=False, max_length=255, verbose_name="Unit S/N")
unitHours = models.DecimalField(null=True, blank=False, decimal_places=2, max_digits=5, verbose_name="Unit Hours")
visualInspection = models.BooleanField(default=False, verbose_name="Visual Inspection")
...
class InstallationReportPartDefinition(models.Model):
deviceType = models.CharField(max_length=3, choices=DeviceProfile.TYPE_DEVICE, default='E', verbose_name="Device Type")
productCode = models.CharField(max_length=32, blank=False, null=False, unique=True, verbose_name="Product Code")
itemDescription = models.CharField(max_length=1023, blank=False, null=False, verbose_name="Item Description")
quantity = models.CharField(max_length=255, blank=False, null=False, verbose_name="Quantity")
class InstallationReportPartInstance(models.Model):
definition = models.ForeignKey(InstallationReportPartDefinition, on_delete=models.CASCADE, related_name="instances", verbose_name="Definitions")
installationReport = models.ForeignKey(InstallationReportData, on_delete=models.CASCADE, related_name="parts", verbose_name="Installation Report")
received = models.BooleanField(default=False, blank=True, verbose_name="Received")
installed = models.BooleanField(default=False, blank=True, verbose_name="Installed")
我有InstallationReportData
的表单,应该如下所示:
Other form fields...
| Product Code | Description | Quantity | Received | Installed |
| xxx1 | desc1 | 2 items | YES | NO |
| xxx2 | desc2 | 1 set | NO | NO |
| xxx3 | desc3 | 2 items | YES | NO |
所以,观点是这样的:
class InstallationReportView(BaseCreationView):
model = InstallationReportData
form_class = InstallationReportDataForm
template_name = 'mism_web/forms/installation_report.html'
permission_denied_message = "You do not have permission to fill installation report data."
parts = None
# Create and set needed parts as instance variable.
def get_initial(self):
super(InstallationReportView, self).get_initial()
device_type = self.workflow.device.device_type
if device_type is not None:
self.parts = InstallationReportPartDefinition.objects.filter(deviceType=self.workflow.device.device_type)
# set context for parts
def get_context_data(self, **kwargs):
context = super(InstallationReportView, self).get_context_data(**kwargs)
context['parts'] = [InstallationReportPartInstance(definition=p, installationReport=None, received=False, installed=False) for p in self.parts]
return context
零件的模板代码(在表单内)如下所示:
Other form fields...
<table class="bordered">
<thead>
<tr>
<th>Product Code</th>
<th>Item Description</th>
<th>Quantity</th>
<th>Received</th>
<th>Installed</th>
</tr>
</thead>
<tbody>
{% for part in parts %}
<tr>
<td>{{ part.definition.productCode }}</td>
<td>{{ part.definition.itemDescription }}</td>
<td>{{ part.definition.quantity }}</td>
<td>
<div>
<input id="id_part_received_{{ part.definition.pk }}" type="checkbox" />
<label for="id_part_received_{{ part.definition.pk }}"></label>
</div>
</td>
<td>
<div>
<input id="id_part_installed_{{ part.definition.pk }}" type="checkbox" />
<label for="id_part_installed_{{ part.definition.pk }}"></label>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
现在,我不知道如何在InstallationReportData
方法中设置form_valid
表单的部件实例值。你能帮我理解如何完成这项工作吗?我被困了一天,我无法弄清楚如何做到这一点。