Converting queryset filter() strings to their fields?

2009-02-06 Thread Ben Gerdemann

Hello,

I think the best why to explain my question is with an example. I have
a model T60CursoAdmin and a query string "a21__a21_ano" that I can use
to search with like this:

T60CursoAdmin.objects.filter(a21__a21_ano=2008)

The models look like this:

class T60Curso(models.Model):
a21 = models.ForeignKey(T21Turma,verbose_name="Turma")
ex = models.CharField()

class T21Turma(models.Model):
a21_ano = models.IntegerField("Ano")

My question is how, can I can get a reference to the 'a21_ano' field
that this filter is referencing? If the filter didn't span through the
foreign key, I could just do:

T60Curso._meta.get_field("ex")

but this doesn't work:

T60Curso._meta.get_field("a21__a21_ano")

Thanks for any and all help.

Cheers,
Bem
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Converting queryset filter() strings to their fields?

2009-02-06 Thread Russell Keith-Magee

On Sat, Feb 7, 2009 at 5:05 AM, Ben Gerdemann  wrote:
>
> My question is how, can I can get a reference to the 'a21_ano' field
> that this filter is referencing? If the filter didn't span through the
> foreign key, I could just do:
>
> T60Curso._meta.get_field("ex")
>
> but this doesn't work:
>
> T60Curso._meta.get_field("a21__a21_ano")

get_field() only works on the local model, so you're going to need to
split the string on '__', and walk the model definitions yourself.
When you find a ForeignKey, you can get the model on the other end,
which gives you another _meta object, which you can call get_field()
on, and so on.

Yours,
Russ Magee %-)

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---