Reputation: 834
I have a model Client and need to get registration addres in VALUES()
class Client(models.Model)
addresses = generic.GenericRelation(Addres, null=True, blank=True )
name =models.CharField(max_length=50)
tel =models.CharField(max_length=20)
...
def get_addres(self):
''' Returns registration addres
'''
I want to do some thig like this
list = Client.objects.all().values( 'name', 'tel', 'get_addres')
Please, tell me how can I solve this issue?
Upvotes: 1
Views: 4033
Reputation: 118448
You can't query python values with django's ORM (it speaks SQL to the database, not django models).
You can collect extra information by using the extra
method on a QuerySet
with SQL.
https://docs.djangoproject.com/en/dev/ref/models/querysets/#extra
I'd prefer trying to build this in Python before resorting to SQL. Unless the reason you're using values()
is specifically to address actual memory issues, I'd just generate the data you need with python.
list = [{'name': x.name, 'tel': x.tel, 'address': x.get_address()}
for x in Client.objects.all()]
Upvotes: 3