1

I'm getting the boolean value from a queryset like this:

activo = Carros.objects.all().values_list('is_active', flat=True).filter(nombre='carro')

then my if statement is this:

if activo == True:
   raise forms.ValidationError('El Auto está activo')

even though the variable 'activo' gets a True value it does not return the ValidationError.

What I did was to set a variable

a = True

and then add it in the if statement

if activo == True:
   raise forms.ValidationError('El Auto está activo')

And it works.

In the Python shell whenver I excute the query I see the result lik this

<QuerySet [True]>

I'm not sure what is the problem.

the 'is_active' field in the model:

is_active = models.BooleanField(blank=False, null=False, default=True)

Note: DB is Postgresql

Jorge López
  • 489
  • 3
  • 17

1 Answers1

1

Add 'use_pure': True to database OPTIONS. This forces mysql-connector-python to use pure python connection instead of C extension, where I believe the bug lives, see here

DATABASES = {
    'default': {
        'ENGINE': 'mysql.connector.django',
        'NAME': os.environ['MYSQL_DATABASE'],
        'USER': os.environ['MYSQL_USER'],
        'PASSWORD': os.environ['MYSQL_PASSWORD'],
        'OPTIONS': {
            'use_pure': True,
    }
}

Or alternatively you can set this in the settings.py file

'OPTIONS': {'use_pure': True }

Edit:

Since your output is retuning a queryset you will need to check the value to ensure that it's true. you can do it like so

activo = <your query set>
print(activo[0])#should return True

if activo[0]:
   raise forms.ValidationError('El Auto está activo')


AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35