1

My method accepts a string parameter which is basically the name of a db column.

can I do something like this:

  original_message = "Let it go"   
  language = "english"
  ads = Ad.objects.filter(language=original_message)

where language is not a name, but a reference to a string? This will save me many if else rows

Dejell
  • 13,947
  • 40
  • 146
  • 229
  • Yes, but I searched for a similar question before, and this didn't come up. So I wouldn't be able to pick up the question neither the answer. My question is very specific and I believe many users will find it useful – Dejell Nov 22 '16 at 15:38
  • Perhaps a better duplicate would help? [How to dynamically provide lookup field name in Django query?](http://stackoverflow.com/q/1227091/1324033) – Sayse Nov 22 '16 at 15:39
  • yep. that's the same – Dejell Nov 22 '16 at 15:39
  • do you want to update the duplicate question so i can mark it that solved my problem? – Dejell Nov 22 '16 at 15:44
  • I can't modify what I've voted to close as but I'm sure others may wish to cast a close vote. Glad it helped though! – Sayse Nov 22 '16 at 15:47

1 Answers1

5

Filter accepts a list of keyword args so you can just use that to your advantage - coupled with unpacking

original_message = "Let it go"   
language = "english"
ads = Ad.objects.filter(**{language:original_message})
Sayse
  • 42,633
  • 14
  • 77
  • 146