0

I have this Postgresql query:

select a.code from accounts a where a.code like '0077-7575%' or '0077-7575' like a.code||'%'

At django I am trying to do this:

q = Accounts.objects.filter(Q(code__startswith='0077-7575') | Q('0077-7575'__startswith=code))

The thing is I don't know and can't find a way to translate '0077-7575' like a.code||'%' into django since a.code is the field name... How can I solve this?

3
  • Doesnt work is too vague. Specify your issue. Any error? Wrong result like that Commented Jun 7, 2020 at 17:48
  • @ArakkalAbu I can't figure it out how to query '0077-7575' like a.code||'%' in Django, keeping in mind a.code is a field. Commented Jun 7, 2020 at 17:56
  • What was the result when you tried with the Django ORM? Commented Jun 7, 2020 at 18:03

1 Answer 1

1

If i am guessing correctly (that you are trying to check that your field is contained inside '0077-7575', you could try something written in this answer.

Basically you would have to do smth like:

Accounts.objects \ 
  .annotate(querystring=Value('0077-7575', output_field=CharField())) \ 
  .filter(querystring__contains=F('code'))
Sign up to request clarification or add additional context in comments.

Comments

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.