I’d like to query a model for a specific field I’ve got as a string.
Demo:
class Foo(models.Model):
someones_name = models.CharField(max_length=100)
anothers_name = models.CharField(max_length=100)
# python shell:
>> Foo.objects.get(pk=1).someones_name
u'Cpt. Hook'
>> Foo.objects.get(pk=1).anothers_name
u'Peter Pan'
Now – I got in a variable the name of the field someones_name
>> field_name = 'someones_name'
So, how do I perform the almost same query against the Foo-Model with that field_name-variable? If indeed such a thing is possible?
Edit
I’m not looking to query with that field_name. I want the exact value of the field given in field_name. I know that I could do something like this
query = Foo.objects.get(pk=1)
query.someones_name
query.anothers_name
All I need is to get this one specific field I dynamicaly receive from ._meta.fields (from another model) as a string.
In RAW-SQL something like: 'SELECT %s FROM foo where pk = 1' % field_name
It is equal to:
UPDATE:
Equal to: