python - 如何获取外键 django 引用的特定 id 的数据?

标签 python django django-3.0

有一个所有问题的列表,正确和不正确(如果问题是错误的和正确的,则得分),每个问题都附有“添加”按钮。

当我点击添加按钮时,问题会被添加到 QuestionQuiz 的测验中,model 连同他们的分数正确的得到 +1 和错误的 -1 被更新到数据库中。

相应的测验 ID 通过以下附表 View 图像中的 url 发送:path('filter_list/<pk>',views.FilterQuestionView.as_view(),name='filter'),

What I want to achieve here is when page loads initially I want to show the scores for the corresponding question based on the quiz id fetched from database. How can I get all question title and description and if question added to quiz or not

What would be django way to achieve this ?



可能有一个获取所有数据的方法,包括那些已经添加到测验中的数据,就像它当前正在获取的数据一样title,description其余字段是静态的

我可以更改获取 title,decription,correct,incorrect,flag(true if added to current quiz_id else false)
附上图片

Here

假设在测验中添加了一个问题。按钮中的文字必须是 已添加 正确和错误必须在页面最初加载时更新。这里第一个问题被添加到测验中。
注意:Value = 1 是默认值,由用户更新正确和不正确。

以下是我创建的模型:

class Question(models.Model):
	title = models.CharField(max_length=255 )
	description = models.TextField(max_length=300)
  

class Quiz(models.Model):
	name = models.CharField(max_length=225,blank=False )
	quiz_type =models.IntegerField(choices=QUIZ_TYPE,default=0)
    questions = models.ManyToManyField( Question, through='QuestionQuiz', related_name="quiz_question") 
	categories= models.ManyToManyField(Category,through='CategoryQuiz',related_name='quiz_category')		
 

class QuestionQuiz(models.Model):
	quiz = models.ForeignKey(Quiz,on_delete=models.CASCADE)
	question = models.ForeignKey(Question,on_delete=models.CASCADE)	
	correct =models.DecimalField(max_digits=4, decimal_places=3)
    incorrect= models.DecimalField(max_digits=4, decimal_places=3) 

	class Meta:
		unique_together = ('quiz','question')


Views.py - 使用 django_filter 包来创建过滤器

class FilterQuestionView( AddQuestionAjaxFormMixin,FormMixin,FilterView):
 
	form_class = QuestionQuizForm
	filterset_class =  QuestionFilter
	paginate_by = 5
	context_object_name = 'questions'

这是模板:

{% for question in questions %}
   		<tr>
        <form method='POST'   id="q_{{question.id}}"> 
   		<td>{{question.title}}</td>
   		<td>{{question.description}}</td>
   	 

   		 
      <td><input type="text" value="1" id="correct_{{question.id}}"></td>

      <td><input type="text" value="1" id="incorrect_{{question.id}}"></td>
      <td> 
            <a id="btn_{{question.id}}" class="btn purple-gradient" onclick="addRemoveQuestion({{question.id}},{{quiz}})"  >Add</a>
       </td>
      </form>

最佳答案

您在这里使用 django_filter。您可以在 QuestionFilter 中覆盖其 qs 函数。这可以帮助您以自己的方式扩展查询。返回的结果可能包含一个标志,表明它是否已添加到测验中。链接引用包含 qs 函数扩展详细信息。 qs function
def qs(self): parent = super().qs d= parent.prefetch_related('quizquestion') return d

关于python - 如何获取外键 django 引用的特定 id 的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61212541/

相关文章:

python - 覆盖 python-social-auth 内置 url?

Django 3静态文件不是注册标签库

python - 如何在 Django 中使用单个表单创建用户和配置文件?

python - Django REST框架: Using the Accept-Language header to set an instance's "locale"

python - 使用plotly模块时PyCharm显示导入错误?

python - Django Save 创建两条记录而不是一条

python - 为什么我不能在 python 中反转列表列表

Django按首字母分组查询集?

python - 用于大量文件的 Django 文件系统存储

python - Python 中的正则表达式 - 具有单个 "re.sub"调用的子字符串