0

I am new to Django and I have a problem that I couldn't solve. I am trying to display a specific question and other related attribute from my Question model based on a field from the Participant model. The issue here is that it directly goes to the else statement even when the condition is true.I tried to print(participant.condition) and it works so I am not sure why its not working with the if statement.

@login_required
def LPSC_VIEW1(request):
    participant=request.user.participant
    if participant.condition == 'LPN':
        First_question= Question.objects.get(id=1)
        all_choices = First_question.choices.all()
        context = {'First_question': First_question, 'all_choices': all_choices}
        return render(request, 'study/FirstQN.html', context)
    else:
        First_question= Question.objects.get(id=12)
        all_choices = First_question.choices.all()
        context = {'First_question': First_question, 'all_choices': all_choices}
        return render(request, 'study/FirstQSC.html', context)

my models as the following:

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    caption = models.CharField(max_length=200, default="this is a caption")
    choices = models.ManyToManyField(Choice)
    vis_image = models.ImageField(default= "this is an image", null=False, blank=False, upload_to="static/study/img")
    def __str__(self):
        return self.question_text


class Condition(models.Model): 
    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name


class Participant(models.Model): 
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    condition = models.ForeignKey(Condition, on_delete=models.CASCADE)
    score = models.IntegerField(default=0)

    def __str__(self):
        return self.user.username
1
  • 1
    does if participant.condition.name == 'LPN': work? Commented Mar 31, 2020 at 13:40

3 Answers 3

2

condition is a foreign key, not a string. You're comparing it against 'LPN', but no instance of your Condition model will be equal to that string.

Try if participant.condition.name == 'LPN': to compare the name field on the Condition instance to that string.

Your print statement shows them as apparently being the same because you've defined how to present Condition instances as strings with your __str__ method - it will print the name for the Condition instance, but that doesn't mean that the Condition value is actually equal to that string.

Sign up to request clarification or add additional context in comments.

Comments

2

You must change this:

participant=request.user.participant

to:

participant=Participant.objects.get(user=request.user)

Comments

0

You might have to use

from .models import Participant
participant = Participant.objects.get(user = request.user)

3 Comments

already on my view and participant has been created in another view
Django queries the database at the time of render, if it is in a different view it won't be accessed in another, you have to query it everytime you need it
thanks, but this did not solve the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.