1

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?

Daler
  • 814
  • 14
  • 28

1 Answers1

3

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()] 
Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245