Tuesday, December 27, 2011

Re: All models with FKs, M2Ms, or 121s pointing to a given model

On Wed, Dec 28, 2011 at 10:01 AM, Daniel Kaplun <min@dvir.us> wrote:
> How do I get all models with FKs, M2Ms, or 121s pointing to a given
> model?

Looking at your pastebin, you've already discovered the _meta object
-- you just needed to dig around a little more in there. The
methods/attributes you're looking for are:

MyObject._meta.fields
- a list of all data, FK and O2O fields on MyObject

MyObject._meta.many_to_many
- a list of all many to many fields on MyObject

MyObject._meta.get_all_related_objects()
- a method returning a list of RelatedObject objects, which define
the model and field that has an FK or O2O relation with MyObject

MyObject._meta.get_all_related_many_to_many_objects()
- a method returning a list of RelatedObject objects, which define
the model and field that has an M2M relation with MyObject

These aren't currently documented, because technically they're not
stable API. However, they haven't changed in almost 6 years, and if we
*were* to make a change, it would likely have a big impact on a lot of
code in the wild. As a result, you can effectively treat them as
pretty-much-stable, but undocumented API.

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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment