Search code examples
pythondjangodjango-q

'Q' object has no attribute 'split' - Django


I have a Model:

class Authors(models.Model):
   name = models.TextField()
   person = models.ForeignKey(Person)

and query:

authors = Author.objects.filter(
                                (Q(name__iregex=r"\y{0}\y".format(s1)),
                                ~Q(name__iregex=r"\y{0}\y".format(s2))
                                ),
                                person=None).order_by('-id')

I am getting the error:

'Q' object has no attribute 'split'

why is this? i am not using split() though.. the line of error is in this query line.


Solution

  • I think you need to join your Q() filters with a logical operator like | or &.

    authors = Author.objects.filter(
                                    (Q(name__iregex=r"\y{0}\y".format(s1)) &
                                    ~Q(name__iregex=r"\y{0}\y".format(s2))
                                    ),
                                    person=None).order_by('-id')