On Mon, 2008-02-11 at 17:33 -0800, [EMAIL PROTECTED] wrote:
> I'm writing a few generic views, in particular a search view.
> 
> It might not know the model that it is working with, so... is there a
> way to determine if one model is related to another?
> 
> for instance:
> 
> model1.isRelated(model2)
> 
> when i say "is related" i mean that either it has a foreign key to
> that model or vice versa

You can't do this quite so directly. However, with a bit of poking about
under the covers you should be able to write your own version.

        model1._meta.fields
        
will give you all the fields directly declared on model1 (except for
ManyToManyFields). You can run through that list looking for things that
are ForeignKey instances. For the reverse relations,

        model1._meta.get_all_related_objects()
        
will return the list of models with relations (ForeignKey, OneToOne) to
the current model.A bit of playing around in the shell looking at dir()
of the returned objects should give you some clues as to how to proceed.

Regards,
Malcolm

-- 
Atheism is a non-prophet organization. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
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