0

例如,我有以下型號:Django的:如何將模型中的形式香脆形式的外鍵在線表單集

class Project(models.Model): 
    description = models.CharField(max_length = 200, null=True) 
    login_date = models.DateField(null=True) 
    login_by = models.CharField(max_length = 200, null=True) 
    notes= models.CharField(max_length = 200, null=True) 

class Sample(models.Model): 
    project = models.ForeignKey(Project, on_delete = models.CASCADE, null=True) 
    serial_number = models.CharField(max_length = 200, null=True) 
    location = models.CharField(max_length = 200)  

class ProjectForm(ModelForm): 
    login_date=forms.DateField(widget=DateWidget(attrs={'id':"login_sample"}, usel10n = True, bootstrap_version=3), initial=datetime.date.today) 
    in_notes = forms.CharField(widget=forms.Textarea) 


    helper = FormHelper() 
    helper.layout = Layout(
     Div(
      'description', 
      'login_date', 
      'login_by', 
      'in_notes', 
      ButtonHolder(
       HTML("<button type='submit' class='save btn btn-default' name = 'projectform'>Save</button>"), 
      ), 
     ) 
    ) 

    class Meta: 
     model = Project 
     fields = ['description', 'login_date','login_by', 
     'in_shipping_method_tracking_number','in_notes'] 

class SampleDetailForm(ModelForm): 
    helper = FormHelper() 
    helper.layout = Layout(
     Div(
      'serial_number', 
      'location', 
      ButtonHolder(
       HTML("<button type='submit' class='save btn btn-default' name = 'sampleform'>Save</button>"), 
      ), 
     ) 
    ) 

    class Meta: 
     model = Sample 
     fields = ['octa_number', 'serial_number', 'pctest_number', 'location',] 

這是我目前正在建立的模型和形式。

我在想如何製作一個ProjectForm,它不僅有自己的領域,而且還有一個SampleForm的內聯formset,其中多個樣本可以在模型表單中添加/刪除,就像我們在管理中現場。我也很高興知道我應該如何處理保存視圖和渲染模板。

TIA

回答