URL的CreateView和外键

时间:2020-01-26 07:32:49

标签: django-templates django-views

我正在尝试将外键从URL传递到CreateView。没有任何关于如何执行此操作的出色文档。 InvoiceCreateView将仅从AssessmentListView创建记录,因此始终必须发送外键。我似乎找不到任何有关如何重写基于通用类的视图的好的文档,如果有人可以给我链接或指导的话,请。

这是我的模特

class Assessment(models.Model):
    Source = models.ForeignKey(Source, on_delete = models.CASCADE)
    Report_Type = models.ForeignKey(ReportType, on_delete=models.CASCADE, null=True, blank=True)
    Clinic = models.ForeignKey(Clinic, on_delete=models.CASCADE)
    Doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE)
    Agent = models.ForeignKey(Agent, on_delete=models.CASCADE, blank=True, null=True)
    Claimant_Name = models.CharField(max_length=100)
    Claim_Number = models.CharField(max_length=20, null=True, blank=True)
    AssessmentDate = models.DateField(null=True, blank=True)
    AssessmentTime = models.TimeField(null=True, blank=True)    


    def __str__(self):
        return self.Claimant_Name

    def get_absolute_url(self):
        return reverse('assessments-detail', kwargs={'pk': self.pk})


class Invoice(models.Model):
    Assessment = models.ForeignKey(Assessment, on_delete = models.CASCADE)
    Number = models.IntegerField(null=True, blank=True)
    Date = models.DateField(null=True, blank=True)
    Subtotal = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    Tax = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    Total = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)

    def __str__(self):
        return self.Assessment

    def get_absolute_url(self):
        return reverse('invoices-detail', kwargs={'pk': self.pk})

View.py

class InvoiceCreateView(CreateView):
    model = Invoice
    template_name = 'intake/invoice_form.html'
    form_class = InvoiceForm

    def form_invalid(self, InvoiceForm):
        InvoiceForm.instance.Assessment.id = self.kwargs.get('pk')
        return super(InvoiceCreateView, self).form_valid(InvoiceForm)

Forms.py

class AssessmentForm(forms.ModelForm):
    class Meta:
        model = Assessment
        fields = '__all__'
        widgets = {'AssessmentDate': DateInput()}

class InvoiceForm(forms.ModelForm):
    class Meta:
        model = Invoice
        fields = '__all__'
        widgets = {'Date': DateInput()}

模板

<tbody>
        {% for item in assessments %}
        <tr>
            <td><a href="{% url 'assessments-detail' item.pk %}">{{ item.Source }} - {{ item.Report_Type }}</a></td>
            <td>{{ item.Claimant_Name }}</td>
            <td>{{ item.Doctor }}</td>            
            <td>{{ item.AssessmentDate }}</td>
            <td>{{ item.AssessmentTime }}</td>
            <td>{{ item.Clinic }}</td>
            <td><a class="btn btn-ou`enter code here`tline-primary btn-sm" href="{% url 'invoices-new' item.pk %}">+Invoice</a></td>
        </tr>
        {% endfor %}

    </tbody>

URL.py

path('invoices / new //',InvoiceCreateView.as_view(),name ='invoices-new'),

0 个答案:

没有答案