0

I am trying to filter the objects based on many to many field.

profiledata = UserProfile.objects.get(user=newdata)
role = profiledata.role
ib = IdealBehaviour.objects.filter(cbs_role = cbsrole)
a_questions = Questions.objects.filter(Q(role=role) & Q(ideal_behaviour=ib))

Here i am not getting a_questions. While doing this i am getting error.

(1242, 'Subquery returns more than 1 row')
Django Version: 1.5.1
Exception Type: DatabaseError
Exception Value:    
(1242, 'Subquery returns more than 1 row')
Exception Location: /usr/lib/python2.7/dist-packages/MySQLdb/connections.py in defaulterrorhandler, line 36
Python Executable:  /usr/bin/python
Python Version: 2.7.6

I tried using _in lookup also

a_questions = Questions.objects.filter(Q(role=role) & Q(ideal_behaviour_in=ib))

Getting error

Cannot resolve keyword 'ideal_behaviour_in' into field. Choices are: created_time, id, ideal_behaviour, question, role

I dont know what wrong i am doing. ib is getting filtered properly as IdealBehaviour has many to many relationship with CBSROLE Please help me out for this.

1 Answer 1

4

you need double underline at __in and you dont need Q here.

a_questions = Questions.objects.filter(role=role, ideal_behaviour__in=ib)
Sign up to request clarification or add additional context in comments.

4 Comments

how can i filter the object depending on ideal_behaviour__in which i dont want. ideal_behaviour__in!=ib is it right?
@Gaurav you can something like .exclude(ideal_behaviour__in=ib).filter(role=role)
So the statement will be a_questions = Questions.objects.exclude(ideal_behaviour__in=ib).filter(role=role)
Thanks a lot for your priceless help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.