I use something like this in a function that creates forms for most  
of my models with some special processing based on related fields:

for n in object_instance._meta.fields:
     if n.verbose_name == 'RELATED_FOREIGNKEY_MODEL_VERBOSE_NAME':
         #Model has FK relation
for n in object_instance._meta.many_to_many:
     if n.verbose_name == 'RELATED_M2M_MODEL_VERBOSE_NAME:
         #Model has m2m relation

the ._meta.fields and ._meta.many_to_many are attributes of the model  
itself so:

Model._meta.fields and Model._meta.many_to_many work as well
You can also simply introspect the instance itself to find out the  
model worked with:
object_instance._meta.verbose_name

On Feb 11, 2008, at 7:33 PM, [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 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