python - Django 中 CreateView 的一些问题

标签 python django django-generic-views django-class-based-views

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)
        patient = Patient.objects.get(id=1)
        context["patient"] = patient
        return context

    def post(self, request, *args, **kwargs):
        analysis = Analyzes()
        sid = transaction.savepoint()
        analysis.name = request.POST["name"]
        analysis.patient_id = Patient.objects.get(id=1)
        analysis.who_send = request.POST["who_send"]
        analysis.who_is_doctor = request.POST["who_is_doctor"]
        analysis.lab_user_id = Doctor.objects.get(id=request.POST["lab_user_id"])
        analysis.additional_lab_user = request.POST["lab_user_add"]
        analysis.date = '2017-06-18'
        analysis.type = 3
        analysis.date_analysis = '2017-06-18'
        analysis.save()
        return super(Biochemical_analysis_of_blood, self).post(request, *args, **kwargs)

我有下一个算法:

  1. 渲染 BiochemicalAnalysisOfBloodForm给用户

  2. 当他填写字段并按下“保存”按钮时,我创建了 Analyzes() 的新实例。并以编程方式填充它,在 post 方法中我调用 super().post()那么用户数据将被写入模型 BiochemicalAnalysisOfBlood自动地?但我有下一个错误:

NOT NULL constraint failed: laboratory_biochemicalanalysisofblood.analysis_id

如何在手动模式下将早期创建的 Analyzes() 实例添加到模型的“分析”字段中?我完全不明白这门课,我可以在哪里找到有关所有机会的信息

最佳答案

算法的主要部分应该驻留在您的表单中,因为您希望将analysis_id传递给正在保存的实例

class BiochemicalAnalysisOfBloodForm(ModelForm):
    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 = '2017-06-18'
        analysis.type = 3
        analysis.date_analysis = '2017-06-18'
        analysis.save()
        # Your analysis is created, attach it to the form instance object
        self.instance.analysis_id = analysis.id
        return super().save(commit)

关于python - Django 中 CreateView 的一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44612705/

相关文章:

python - Django 基于类的 View : How do I pass additional parameters to the as_view method?

django - 如何使用请求数据在基于 Django 类的通用 createview 上设置初始数据

python - Python中的下采样数组

Python:如何从AWS S3读取和加载Excel文件?

python - 根据列的值在 DataFrame 中填充 NaN

json - django 休息 api : JSON parse error - No JSON object could be decoded

python - 迁移 django 数据库时 azure 用户名无效

Django:每个用户的 UpdateView 限制

python - 查找向量 v (1,k) 在矩阵 M (m,k) 中的出现次数

Django-Bootstrap3 内联表单布局问题