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]:
> [<django.db.models.fields.AutoField object at 0xb7c87bcc>,
>  <django.db.models.fields.related.ForeignKey object at 0xb7c8744c>,
>  <django.db.models.fields.CharField object at 0xb7c8772c>,
>  <django.db.models.fields.IntegerField object at 0xb7c879ac>]
> 
> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to