0

I am taking a Django course on Udemy and have decided to add on to what I've learned by changing the layout of a form with an If statement.

Basically, I want to see if the BooleanField for the variable for_you is set to true (or if the check box in the GUI is checked), but I am having trouble figuring out how to access if the boolean field is set to false. Can anyone point me in the right direction?

class SignUp(models.Model):
for_you = models.BooleanField(default=True, verbose_name="Is this purchase for you? If so, check this box.")
if for_you:
3
  • Be explicit. if for_you == True or if for_you == False. Of course, if you want to keep the coding style that you have: if for_you: or if not for_you Commented Apr 15, 2015 at 2:28
  • @Zizouz212 The latter is simpler, and just as clear if a more sensible variable name is chosen, e.g. if is_for_you:. Commented Apr 15, 2015 at 2:38
  • @figs The former though, has the advantage of being more explicit. Commented Apr 15, 2015 at 2:39

2 Answers 2

1

I think you need to tell us a bit more about the context where you're trying to access for_you.

I'm not sure if this will be helpful for you, but here's how to access for_you depending on where you are in relation to the SignUp class.

Inside a method of SignUp:

class SignUp(models.Model):

    def my_method(self):
        if self.for_you:
            # do stuff

Outside SignUp:

my_sign_up = SignUp()
if my_sign_up.for_you:
    # do stuff
Sign up to request clarification or add additional context in comments.

2 Comments

I really don't know how to ask the question because my lingo isn't up to par (and thus I don't know how to look for the question despite my efforts). the for_you variable is set by an object method. I need a way of checking if the object method is true. Simply checking if for_you == True or not does not produce the desired results. I'm not sure if it makes any sense, but I'm trying ;p
Where are you checking the if for_you == True; inside the model declaration, or outside the model declaration?
1

The most direct way of checking if a boolean is False is using not:

if not for_you:
    print 'for_you is False!'

2 Comments

Strings are quotations!
@Zizouz212 oops trying to format as code in my code block, thanks.

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.