Re: django.db.models import Q help!
On Fri, Oct 21, 2011 at 7:43 PM, Ian Clellandwrote: > On Fri, Oct 21, 2011 at 11:06 AM, Kayode Odeyemi wrote: > >> Hello, >> >> Please I need help with django.db.models.Q. I have the query below which >> is returning empty >> >> qs = >> Q(financial_institution=request.user.get_profile().financial_institution) >> txn = Transaction.objects.extra( >> where=['(tpin=%s or teller_no=%s or identifier=%s) AND >> financial_institution_id=%s'], >> params=[value, str(value), value, str(qs)]) >> >> If I substitute the line params=[value, str(value), value, str(qs)]) with >> params=[value, str(value), value, 1), >> I get real results from the database, meaning the problem lies with Q. >> >> > Why are you trying to use a Q object for this? Q objects are meant to be > passed into .filter(), not coerced into extra SQL. > > If what you need for the params is the ID of another object, then just use > that! > > Try something like > > fi = request.user.get_profile().financial_institution > > txn = Transaction.objects.extra( > where=['(tpin=%s or teller_no=%s or identifier=%s) AND > financial_institution_id=%s'], > params=[value, str(value), value, fi.id]) > > fi is expected to return a single result. i got it fixed like this: txn = Transaction.objects.extra( where=['tpin=%s or teller_no=%s or identifier=%s AND financial_institution_id=%s'], params=[value, str(value), value, fi]) removed the parenthesis that encapsulates tpin, teller_no and identifier. Thanks > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- Odeyemi 'Kayode O. http://www.sinati.com. t: @charyorde -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: django.db.models import Q help!
On Fri, Oct 21, 2011 at 11:06 AM, Kayode Odeyemiwrote: > Hello, > > Please I need help with django.db.models.Q. I have the query below which is > returning empty > > qs = > Q(financial_institution=request.user.get_profile().financial_institution) > txn = Transaction.objects.extra( > where=['(tpin=%s or teller_no=%s or identifier=%s) AND > financial_institution_id=%s'], > params=[value, str(value), value, str(qs)]) > > If I substitute the line params=[value, str(value), value, str(qs)]) with > params=[value, str(value), value, 1), > I get real results from the database, meaning the problem lies with Q. > > Why are you trying to use a Q object for this? Q objects are meant to be passed into .filter(), not coerced into extra SQL. If what you need for the params is the ID of another object, then just use that! Try something like fi = request.user.get_profile().financial_institution txn = Transaction.objects.extra( where=['(tpin=%s or teller_no=%s or identifier=%s) AND financial_institution_id=%s'], params=[value, str(value), value, fi.id]) -- Regards, Ian Clelland -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
django.db.models import Q help!
Hello, Please I need help with django.db.models.Q. I have the query below which is returning empty qs = Q(financial_institution=request.user.get_profile().financial_institution) txn = Transaction.objects.extra( where=['(tpin=%s or teller_no=%s or identifier=%s) AND financial_institution_id=%s'], params=[value, str(value), value, str(qs)]) If I substitute the line params=[value, str(value), value, str(qs)]) with params=[value, str(value), value, 1), I get real results from the database, meaning the problem lies with Q. I tried to filter qs like this: txn = Transaction.objects.filter(qs), but I got some errors that Q cannot be filtered. Any help will be much appreciated. Thanks -- Odeyemi 'Kayode O. http://www.sinati.com. t: @charyorde -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.