1
私はInstallationReportPartInstance
のような3つのモデルクラスがInstallationReportData
と多対1の関係にあります。リアルタイムで、多対一フィールドをdjango形式に統合する方法
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
:
ので、ビューはこのようなものです
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...
<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>
、私はform_valid
方法でInstallationReportData
フォームの一部のインスタンスの値を設定する方法がわかりません。これをどうやって行うのか理解できますか?私は一日立ち往生しており、これをどうやって行うのか本当に分かりません。
私はこれが私の必要とするものではないと思います。 – nakiya
@nakiyaあなたは精巧にできますか?ユーザーが外来キーの対応するモデルをフォームで選択する方法が必要ですか? – ubadub
@udadub:um、私は外来キーに対応する複数のモデルを選択するだけでなく、複数のモデルの値を同時に編集したいと思います。 – nakiya