gpt4 book ai didi

python - 跨多个模型的django访问字段

转载 作者:行者123 更新时间:2023-11-28 00:19:26 24 4
gpt4 key购买 nike

我在我的 Django 应用程序中使用以下模型,并希望跨多个字段进行查询。我环顾了不同的地方,但找不到我真正需要的东西。

class Attempt(models.Model, object):
'''Creates an Attempt Entity which is a subclass of models.Model class'''
attemptID_f = models.AutoField(primary_key=True)
questionID_f = models.ForeignKey(Question, verbose_name="Question", null=False)
userID_f = models.ForeignKey(User, verbose_name="User ID", null=False)
solution_f = models.TextField("Solution uploaded by the User", null=False)
errorReportID_f = models.ForeignKey(ErrorReport,verbose_name="Error Report for the Solution", null=True)
status_f = models.BooleanField("Status of attempt - true = right, false = wrong", blank=True, default=False)
timeOfSubmission_f = models.DateTimeField("Time of Submission", null=False)
compilerVersion_f = models.ForeignKey(CompilerVersion, verbose_name = "Compiler version of the Attempt",null=False)

class Question(models.Model, object):
'''Creates the entity question
which is a subclass of models.Model'''
questionID_f = models.AutoField(primary_key=True)
questionText_f = models.TextField("Problem Statement", null=False)
questionTitle_f = models.CharField("Problem Title", max_length = 50, null = False)
level_f = models.ForeignKey(Level, verbose_name="Question Level", null=False)
type_f = models.ForeignKey(Type, verbose_name="Type of Question", null=False)
timeLimit_f = models.FloatField("Time Limit for Question",null=False)

class Type(models.Model):
'''Creates the entity Type which is a subclass of models.Model class'''
typeID_f = models.AutoField(primary_key=True)
typeName_f = models.CharField("Type Name" , max_length = 30 , null = False)

typesm = Attempt.objects.filter(userID_f = request.user).values('attempt__questionID_f__type_f__typeID_f')

如果我想引用 MODEL 类型的 typeID_f 字段,attempt__questionID_f__type_f__typeID_f 是否是一个有效的论点,该字段由问题模型的 type_f 字段引用, 由 Attempt Model 的 questionID_f 字段引用 ?

如有任何帮助,我们将不胜感激。

谢谢,

潘卡杰。

最佳答案

应该是:

typesm = Attempt.objects.filter(userID_f = request.user).values('questionID_f__type_f__typeID_f')

我不确定为什么在查询 Attempt 模型时要在此处放置 attempt__ 前缀。

参见:Lookups that span relationships

关于python - 跨多个模型的django访问字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9999412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com