Search code examples
djangopostgresqldjango-modelsormpgadmin-4

Can't filter objects by BooleanField in Django


I'm building a reminder and trying to filter Task objects by a BooleanField called 'status'.

as you can see, I have assigned Task.objects.filter(status=False) to undone_tasks, but when I run it, I get this error: ProgrammingError at /showing-all-tasks/ argument of NOT must be type boolean, not type character varying LINE 1: ..."status", "task"."reminder" FROM "task" WHERE NOT "task"."st...

This is the query running in postgres: SELECT "task"."id", "task"."user_id", "task"."title", "task"."slug", "task"."description", "task"."deadline_date", "task"."date_edited", "task"."status", " task"."reminder" FROM "task" WHERE NOT "task"."status" ORDER BY "task"."deadline_date" DESC

when I change "task"."status" to "task"."status" = 'true' in pgAdmin, everything works fine! but im cofused! First of all: why do I get this error? secondly: if status is a BooleanField, why when I compare it a string('true') I don't receive any errors?

thanks for the help!

views.py

class TaskListView(ListView):
    model = Task
    template_name = 'mysite/show_tasks.html'

    def get_context_data(self, **kwargs):
        context = super(TaskListView, self).get_context_data()
        undone_tasks = Task.objects.filter(status=False)        
        return context

models.py

class Task(models.Model):
  # some other fields...
  status = models.BooleanField(verbose_name='Task Status', default=False)

class ReminderNotification(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='user', related_name='owner_notifications')
    message = models.TextField(max_length=200, default="", blank=True, null=True, verbose_name='message')
    task = models.ForeignKey(Task, on_delete=models.CASCADE, verbose_name='task',
                             related_name='task_reminder_notifications')


Solution

  • This is how I solved this problem: I checked the type of "status" in pgadmin 4, it was character varying instead of Boolean, so I changed its type using the query bellow: ALTER TABLE task ALTER COLUMN status TYPE BOOLEAN USING status::boolean