On Sep 16, 11:30 pm, Kathleen Benitez <[email protected]>
wrote:
> I am attempting to evaluate some generalization schemes using Django.
> In particular, I'd like to create a new field (generalized race), and
> then find a total population using the values() function as follows:

annotate() is somewhat confusing in your example, seeing the Record
model and explicitly stating what you want to achieve with it
would help us to help you better.

> 1  r = Records.objects
> 2  r = r.extra(select={'generalized_race: "CASE race WHEN 'Black' THEN
> 'B' WHEN 'White' THEN 'W' ELSE 'O' END"})

This is indeed "registered with Django":

>>> rs = Record.objects.extra(select={'generalized_race':
... "CASE race WHEN 'Black' THEN'B' WHEN 'White' THEN 'W' ELSE 'O'
END"})
>>> rs[0].generalized_race
u'B'

However, it is correct that although extra(select={'foo': ...})
augments the results with the field foo, it is not a proper
models.Field and can not be referenced in further QuerySet methods.

> 3  fields = ['age', 'sex', 'generalized_race']
> 4  r = r.values(*fields).annotate(Pop = Sum('population'))

Are you sure you need the values() call here? If you really don't
want to select other fields, only() works equally well and retains
the generalized_race pseudo-field:

>>> fields = ['age', 'sex']
>>> rs.only(*fields)[0].generalized_race
u'B'

As for the annotate() clause, it is unclear where the population comes
from and what's the intent. Please clarify.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to