'Wrong SQL is generated when querying Array of Datetime field in Django

I have a model where I am storing datetime in Array Field. Now when I am using this Array field in F expression this is giving me SQL error. On Printing the SQL I can see that wrong SQL is being Generated.

Here is the Sample MOdel Structure :

class MyModel(models.Model):
       valid_date = models.DateTimeField(default=timezone.now)
       exclude_dates = ArrayField(
                models.DateTimeField(_("Exclude dates"), null=True, blank=True),
                null=True, blank=True
            )

Now When I will query this model to give me all records but ignore those Dates who are in exclude_dates list.

qs = MyModel.objects.exclude(valid_date__range=F('exclude_dates'))

This above ORM will raise psycopg2.errors.SyntaxError. if you will check resultant SQL by print(qs.query). You will find out that wrong SQL is being generated somehow.

I am struggling to find out the reason of same and not able to think about the alternative solution.

SQL : SELECT "mymodel"."id" FROM "mymodel" WHERE "mymodel"."valid_date" BETWEEN " AND v

Django : Django==3.1.2



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source