Python 使用类跨度格式创建对象。可能吗?

Python 使用类跨度格式创建对象。可能吗?,python,django,django-models,django-views,django-queryset,Python,Django,Django Models,Django Views,Django Queryset,为什么span关系查询使用get而不使用create?有什么方法可以让它工作吗 一些背景: 目前我正在做类似的事情 qm = questionModel.object.get(question=quest) answer = modelInterviewAnswer.objects.create(patient=patientobj,question=qm ) 现在我知道这种方法已经奏效了 modelInterviewAnswer.objects.get(patient=patientobj,

为什么span关系查询使用get而不使用create?有什么方法可以让它工作吗

一些背景:

目前我正在做类似的事情

qm = questionModel.object.get(question=quest)
answer = modelInterviewAnswer.objects.create(patient=patientobj,question=qm )
现在我知道这种方法已经奏效了

modelInterviewAnswer.objects.get(patient=patientobj,question__question=quest )
我的问题是,为什么这样的东西只适用于get而不适用于create

更新:

这是我尝试使用时遇到的错误

  modelInterviewAnswer.objects.create(patient=patientobj,question__question=quest )

    'question__question' is an invalid keyword argument for this function

所以我的问题是,为什么question\uu question与get一起工作,但当我将其与create一起使用时,会出现一个异常。

嗯,您必须指定它不工作的含义。从理论上讲,它确实可以工作——您确实可以通过调用RandomClass.objects.createfield1='field-1-value',field2='field-2-value'来创建对象,并且它可以工作。如果它适用于.get,而不适用于.create,我假设您在尝试该代码时遇到某种异常,那么一个原因可能是get从DB检索现有对象,并可以填充DB中所有必需的字段值,而.create将向DB中插入一个新对象,如果缺少某些必需值,然后你会得到一个例外。 另一种方法或解决方案不是使用create,它基本上是一个直接的DB命令,而是使用Django中介模型实例来创建对象。基本上区别如下:

from django.db import models

class RandomModel(models.Model):
    # NB! Required fields
    field1 = models.CharField(max_length=32)
    field2 = models.CharField(max_length=32)


# This is how you want to do it
RandomModel.objects.create(field1='value')
# An error is returned because you didn't specify a value for field2 and this goes directly to DB

# An alternative:
obj = RandomModel(field1='value')
obj.field2 = 'value232'
obj.save() #  Only here is it saved to the DB
如果你在寻找一个更具体的答案,相应地更新你的问题

编辑: 因为字段问题位于另一个模型中,您不能使用一个模型的创建方法更改另一个模型的字段。但是,您可以使用.get、.filter、.exclude等方法根据现有对象的字段值对其进行筛选

要实现你想要的,你必须做到以下几点,这不是唯一的方法,请注意:

# if the question instance exists and you just want to change the question (kind of unlikely in my opinion?)
question_instance = QuestionModel.objects.get(#some-params-that-get-the-question)
question_instance.question = 'How the hell did we end up here?'
question_instance = question_instance.save()
# Proceed to create the other model

# if the question instance does not exist (more likely version)
question = QuestionModel.objects.create(question='Is this more like it?')
answer = AnswerModel.objects.create(answer='Yes, it is.', question=question)

@JamesFranco编辑了答案是的,我现在就是这么做的,我很好奇我们是否可以在像quest__question=xx这样的save方法中使用类似于跨越的查询,结果发现我们不能很遗憾,我是对的吗?
# if the question instance exists and you just want to change the question (kind of unlikely in my opinion?)
question_instance = QuestionModel.objects.get(#some-params-that-get-the-question)
question_instance.question = 'How the hell did we end up here?'
question_instance = question_instance.save()
# Proceed to create the other model

# if the question instance does not exist (more likely version)
question = QuestionModel.objects.create(question='Is this more like it?')
answer = AnswerModel.objects.create(answer='Yes, it is.', question=question)