这可能是一个基本问题,但是我是django的新手。 。
我正在构建一个测验应用程序,允许用户按顺序回答问题。例如,用户只能在成功回答问题1后回答问题2。
我正在使用django.contrib.auth进行用户身份验证,并添加了一个Profile模型来扩展用户信息,包括跟踪每个用户已回答的所有问题。
这是我的模特:
class Question(models.Model):
question_text = models.CharField(max_length=400)
answer1 = models.CharField(max_length=200)
times_solved = models.IntegerField(default=0)
number = models.IntegerField(default=1, unique=True)
def __str__(self):
return self.question_text
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
real_name = models.CharField(max_length=100)
questions_answered = models.ManyToManyField(Question, blank=True, null=True)
last_wrong_answer_made_on = models.DateTimeField('last wrong answer date', null=True, blank=True)
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
我还有一个索引视图,其中列出了所有用户以及他们回答的最后一个问题的编号:
class IndexView(generic.ListView):
template_name = 'piratehunt/index.html'
context_object_name = 'user_list'
def get_queryset(self):
return Profile.objects.all().order_by('-questions_answered__number')
还有我的index.html:
{% if user_list %}
<ul>
{% for user in user_list %}
<li><a href="{% url 'piratehunt:user_detail' user.id %}">{{ team.user.username }} - {{ user.questions_answered.last.number }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No users have signed up.</p>
{% endif %}
我的问题在我的QuestionDetail视图中。每当我尝试更新用户的个人资料以表明他已经解决了一个新问题时,我最终都会为该用户创建一个新的个人资料,而不是简单地更新现有的用户个人资料。这是相关的代码:
@login_required
def QuestionDetail(request, question_number):
user = User.objects.get(pk=request.user.id)
p = user.profile
last_question = user.profile.questions_answered.last()
current_question = Question.objects.get(number=(last_question.number + 1))
form = AnswerForm(request.POST)
if form.is_valid():
attempt = form.cleaned_data.get('answer')
if attempt == current_question.answer1
##########This is what triggers the problem########
user.profile.questions_answered.add(current_question)
p.save()
#make sure to reset the clock so that the team can answer the next question quickly
current_question.times_solved = current_question.times_solved + 1
current_question.save()
messages.info(request, 'Great News! You are correct! You can now go on to the next problem')
return HttpResponseRedirect(reverse('piratehunt:index'))
else:
p.last_wrong_answer_made_on = now()
p.save()
messages.info(request, 'All guesses are wrong! Try again in 2 hours.')
return HttpResponseRedirect(reverse('piratehunt:index'))
else: #this is the GET for this view
return render(request, 'piratehunt/question_answer.html', {'form': form, 'question': current_question.question_text})
最终结果是,一旦用户回答了两个问题,他在index.html中出现2次,而一旦他回答了三个问题,他就出现了3次,依此类推。
这是它的样子:
答案 0 :(得分:1)
您不是要创建新的配置文件,而是查询集:
Profile.objects.all().order_by('-questions_answered__number')
返回重复项,因为number
处于多对多状态。您正在要求一个配置文件,该配置文件将由附加到其上的对象列表上的属性进行排序。您可以在该列表上做注解,例如Min
或Max
,也可以应用.distinct()
。
最小/最大:
from django.db.models import Max
Profile.objects.annotate(
max_number=Max('questions_answered__number')
).order_by('-max_number')
不同:
Profile.objects.order_by('-questions_answered__number').distinct()
我建议您使用注释路线,因为它更一致。而且您也不必在模板中使用{{ user.questions_answered.last.number }}
即可进行另一个数据库查询。