如何从 url 获取变量到表单的方法 - 保存?

how get a variable from url to form's method - save?

我有 urlid 为

的模式
...
url(r'^3/(?P<id>[-\w]+)', Biochemical_analysis_of_blood.as_view(),\
    name='biochemical_analysis_view'),
...

views.py

class Biochemical_analysis_of_blood(CreateView):
    model = BiochemicalAnalysisOfBlood
    form_class = BiochemicalAnalysisOfBloodForm
    template_name = "biochemical_analysis_of_blood.html"
    success_url = reverse_lazy("patients")

    def get_context_data(self, **kwargs):
        context = super(Biochemical_analysis_of_blood, self).get_context_data(**kwargs)
        context["patient"] = Patient.objects.get(id=self.kwargs['id'])
        context["caption"] = 'Біохімічний аналіз крові'
        context["new"] = True
        return context

forms.py

class SaveForms():
    def save(self, commit=True):
        analysis = Analyzes()
        sid = transaction.savepoint()
        analysis.name = self.data["name"]
        analysis.patient_id = Patient.objects.get(id=1)
        analysis.who_send = self.data["who_send"]
        analysis.who_is_doctor = self.data["who_is_doctor"]
        analysis.lab_user_id = Doctor.objects.get(id=self.data["lab_user_id"])
        analysis.additional_lab_user = self.data["lab_user_add"]
        analysis.date = self.data["date"]
        analysis.type = 3
        analysis.date_analysis = self.data["date_analysis"]
        analysis.save()
        # Your analysis is created, attach it to the form instance object
        self.instance.analysis_id = analysis.id
        return super().save(commit)

如何从 url 获取变量 "id" 到表单的方法 - 保存? class SaveForms 将被其他 forms.models class 继承,因为它们都必须具有相同的保存方法。

...
    analysis.patient_id = Patient.objects.get(id=1)
...

而不是“1”我必须使用 url 中的 "id"...请问谁能帮帮我?

class BiochemicalAnalysisOfBloodForm(SaveForms, forms.ModelForm):
...

您可以在您的表单中访问请求数据,查看此答案:

How do I access the request object or any other variable in a form's clean() method?

取 request.path.

而不是 request.user 对象