8

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.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
doniyor
  • 36,596
  • 57
  • 175
  • 260

1 Answers1

9

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')
Sohan Jain
  • 2,318
  • 1
  • 16
  • 17
  • i thought, ``,`` stays for ``&``, doesnot it? – doniyor Apr 03 '14 at 17:39
  • 1
    I think because you have the `Q()` objects wrapped in a paren, you need to use `&` explicitly. I.e, you have `filter( (Q(), Q()), person=None)`. Does changing to `&` work? I'm not entirely confident about this answer TBH haha – Sohan Jain Apr 03 '14 at 17:42
  • @Furbeenator https://docs.djangoproject.com/en/1.7/topics/db/queries/#complex-lookups-with-q-objects – Sohan Jain Nov 19 '14 at 05:02
  • 1
    Thanks, @Sohan Jain. I see they state `Q objects can be combined using the & and | operators. When an operator is used on two Q objects, it yields a new Q object.` but the examples didn't show any `&`s and they are implied with the filter. It would be nice if the docs showed when the explicit `&` is required, rather than have to lookup the "split" error. Thanks again, this got me out of a jam! – Furbeenator Nov 19 '14 at 17:53
  • Actually I still don't see clearly when we have to use the explicit and/or notation and when comma separated Q are enough... – Alex Gidan Feb 02 '15 at 23:51