2017-12-26 14 views
1

다음과 같은 세 가지 모델 클래스가 있습니다. InstallationReportPartInstanceInstallationReportData과 다 대일 관계가 있습니다. 실시간으로,다기간 필드를 장고 양식에 통합하는 방법

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 
: enter image description here

그래서, 뷰는 다음과 같이이다

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") 

I는 다음과 같이한다 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 형태의 부품 인스턴스 값을 설정하는 방법을 모르겠어요. 이 작업을 수행하는 방법을 이해하도록 도와 줄 수 있습니까? 나는 하루 종일 붙어 있었고, 나는 이것을하는 법을 정말로 알 수 없다.

답변

0

가장 쉬운 방법은 수동으로 필드를 생성하는 것이 아니라 Django의 양식 필드를 사용하는 것입니다. 관련 항목은 여기에 있습니다. ModelChoiceField

+0

내가 필요한 것을 제공하지 않는다고 생각합니다. – nakiya

+0

@nakiya 당신은 정교 할 수 있습니까? 사용자가 양식에서 외래 키에 해당하는 모델을 선택하는 방법이 필요합니까? – ubadub

+0

@udadub : 음, 외래 키에 대해 여러 개의 해당 모델을 선택하는 개미뿐만 아니라 여러 모델의 값을 동시에 편집하고 싶습니다. – nakiya