Re: how to make a list of allowable arguments for filter()

2007-02-10 Thread Malcolm Tredinnick

On Fri, 2007-02-09 at 23:22 -0800, abe wrote:
> >
> > I really tried hard to understand your problem, but then my brain
> > started to leak out of my ears and I had to stop. :-(
> >
> > Could you post an example of how all these models are related? You seem
> > to have Relmodl2, model2 and model1 and I'm not sure what all the
> > linkages are.
> >
> > I think what you are asking is why does the reverse end of a ForeignKey
> > show up in get_all_related_objects(), but the forward direction does
> > not. If that is the question, the forward direction (the ForeignKey
> > field itself), is just an object of class ForeignKey in the _meta.fields
> > attribute on the model it is defined on. So you need to inspect the
> > class types in _meta.fields to see which are the forwards relations.
> >
> > If that isn't your question, I apologise and ask only for a simple
> > example to illustrate the problem.
> >
> > Regards,
> > Malcolm
> 
> 
> 
> sorry, shouldn't post so late I guess...
> 
> I would like to make a list of allowable arguments for filter
> to supply that as a selection list on a search page.
> 
> so I have to get a list of 'model__field' like strings
> derived from  models (models.get_all_models())
> 
> 
> class Poll(models.Model):
> question = models.CharField(maxlength=200)
> pub_date = models.DateTimeField('date published')
> 
> 
> class Choice(models.Model):
> poll = models.ForeignKey(Poll)
> choice = models.CharField(maxlength=200)
> votes = models.IntegerField()

OK, that's what I suspected, but thanks for the clear example.

[...]
> Now you seem to say that the answer is in here,
> 
> 
> In [18]: [f for f in Choice._meta.fields]
> Out[18]:
> [,
>  ,
>  ,
>  ]
> 
> which I can see. but how do a make an expression to test for
> foreignkey
> (except for  'fields.related' in `f` which seems a bit clumsy)

One way would be:

   [f for f in Choice._meta.fields if f.rel]

That will catch ManyToMany and OneToOne fields as well. For non-relation
fields, the "rel" attribute is None. For relation fields, it points to
the class that does all the heavy lifting for the relation, but that
isn't important here. Just the fact that f.rel will only be non-None for
relation fields.

> and then of course I would like to find the 2nd (or higher) order
> cases too
> 
> 'model1__model2__field_of_model2'
> 
> any simple ways to find those?

You could operate iteratively: start with a list of all your models that
are related to the current one. Do the same process to those and store
any *new* model names that appear. Repeat until no new names appear.

The reason you need to track new names (versus all names) is to avoid
infinite loops (the simple case being a model that has a ForeignKey to
itself). How to represent loops in your result page is something you'll
need to think about.

Regards,
Malcolm



--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: how to make a list of allowable arguments for filter()

2007-02-09 Thread abe

>
> I really tried hard to understand your problem, but then my brain
> started to leak out of my ears and I had to stop. :-(
>
> Could you post an example of how all these models are related? You seem
> to have Relmodl2, model2 and model1 and I'm not sure what all the
> linkages are.
>
> I think what you are asking is why does the reverse end of a ForeignKey
> show up in get_all_related_objects(), but the forward direction does
> not. If that is the question, the forward direction (the ForeignKey
> field itself), is just an object of class ForeignKey in the _meta.fields
> attribute on the model it is defined on. So you need to inspect the
> class types in _meta.fields to see which are the forwards relations.
>
> If that isn't your question, I apologise and ask only for a simple
> example to illustrate the problem.
>
> Regards,
> Malcolm



sorry, shouldn't post so late I guess...

I would like to make a list of allowable arguments for filter
to supply that as a selection list on a search page.

so I have to get a list of 'model__field' like strings
derived from  models (models.get_all_models())


class Poll(models.Model):
question = models.CharField(maxlength=200)
pub_date = models.DateTimeField('date published')


class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(maxlength=200)
votes = models.IntegerField()

relfieldargs = ['%s__%s' % (m.model._meta.module_name,f.name)
  for m in Poll._meta.get_all_related_objects()
  for f in m.model._meta.fields ]

relfieldargs:
['choice__id', 'choice__poll', 'choice__choice', 'choice__votes']

I can use these as (part of)arguments for filter

Poll.objects.filter(choice__votes__gt=0)


but the other way around doesn't work (from Choice->Poll)

relfieldargs = ['%s__%s' % (m.model._meta.module_name,f.name)
  for m in Choice._meta.get_all_related_objects()
  for f in m.model._meta.fields ]

relfieldargs:
[]

so I need some other way to get those.


Now you seem to say that the answer is in here,


In [18]: [f for f in Choice._meta.fields]
Out[18]:
[,
 ,
 ,
 ]

which I can see. but how do a make an expression to test for
foreignkey
(except for  'fields.related' in `f` which seems a bit clumsy)


and then of course I would like to find the 2nd (or higher) order
cases too

'model1__model2__field_of_model2'

any simple ways to find those?

thanks for your help.


abe


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: how to make a list of allowable arguments for filter()

2007-02-09 Thread Malcolm Tredinnick

On Sat, 2007-02-10 at 00:41 +, abe wrote:
> sorry for the previous post. accidentally pressed send too early.
> 
> I can find the models which have a certain model as a ForeignKey like
> this
> 
> modl=models.get_model('myapp','model1')
> 
> relfieldargs = ['%s__%s' % (m.model._meta.module_name,f.name)
>   for m in modl._meta.get_all_related_objects()
>   for f in m.model._meta.fields ]
> 
> relfieldargs
> Out[  ]:
> ['relmodl2__id',
>  'relmodl2__name',
>  'relmodl2__foo',
>  etc.
>  ]
> 
> where {relmodl2__name:value} etc can be use as argument
> to modl.objects.filter(...)
> 
> this works if model Relmodl2 has model2 as ForeignKey
> but not the other way round. is there an eay way to find
> those model and field names?
> 
> for example  relmodl2.objects.filter({'model1__nr__gt':5})
> 
> where I'm looking for the other allowed strings like  'model1__nr'
> 
> 
> or is there an alltogether easier way to do this.

I really tried hard to understand your problem, but then my brain
started to leak out of my ears and I had to stop. :-(

Could you post an example of how all these models are related? You seem
to have Relmodl2, model2 and model1 and I'm not sure what all the
linkages are.

I think what you are asking is why does the reverse end of a ForeignKey
show up in get_all_related_objects(), but the forward direction does
not. If that is the question, the forward direction (the ForeignKey
field itself), is just an object of class ForeignKey in the _meta.fields
attribute on the model it is defined on. So you need to inspect the
class types in _meta.fields to see which are the forwards relations.

If that isn't your question, I apologise and ask only for a simple
example to illustrate the problem.

Regards,
Malcolm



--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: how to make a list of allowable arguments for filter()

2007-02-09 Thread abe

sorry for the previous post. accidentally pressed send too early.

I can find the models which have a certain model as a ForeignKey like
this

modl=models.get_model('myapp','model1')

relfieldargs = ['%s__%s' % (m.model._meta.module_name,f.name)
  for m in modl._meta.get_all_related_objects()
  for f in m.model._meta.fields ]

relfieldargs
Out[  ]:
['relmodl2__id',
 'relmodl2__name',
 'relmodl2__foo',
 etc.
 ]

where {relmodl2__name:value} etc can be use as argument
to modl.objects.filter(...)

this works if model Relmodl2 has model2 as ForeignKey
but not the other way round. is there an eay way to find
those model and field names?

for example  relmodl2.objects.filter({'model1__nr__gt':5})

where I'm looking for the other allowed strings like  'model1__nr'


or is there an alltogether easier way to do this.


thanks-abe


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



how to make a list of allowable arguments for filter()

2007-02-09 Thread abe

I can find the models which have a certain model as a ForeignKey like
this


relfieldargs = ['%s__%s' % (m.model._meta.module_name,f.name)
for m in
models.get_model('zb','compound')._meta.get_all_related_objects() for
f in m.model._meta.fields]


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---