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

2011-12-28 Thread JohnA
It's maybe not as rigorous as the explicitly _meta-based approach
noted by Russ McGee, but I use straight introspection combined with a
knowledge of naming conventions, as follows:

# This function looks only at passed in object, not its derived
subobjects
# If you want items from the most derived class or whatever, get that
object first
# and then call this function on it.

def getRelatedManagerNamesAndObjects(object,namePrefix=None): #
returns a list of name-value pairs
if namePrefix == None:
namePrefix = ''
namesAndValues = []
for name in dir(object):
if not name.startswith(namePrefix):
continue
try:
value = getattr(object,name)
except:
continue
typeStr = str(type(value))
if typeStr.find('RelatedManager') != -1:
namesAndValues.append((name,value))
return namesAndValues

It doesn't find 121 relationships, only m21 (FK) and m2m, but that's
what I want to find.  I use a similar introspection method to find 121
relationships, which for me all result from multi-level multi-table
inheritance.  So far both methods have worked fine for me.  They may
seem a little inefficient but I don't do them that often and I assume
that the resources they use are nothing compared to the joins that are
needed to get the actual data I'm looking for (i.e. queries made on
the returned related managers).

-- John

On Dec 27, 9:01 pm, Daniel Kaplun  wrote:
> How do I get all models with FKs, M2Ms, or 121s pointing to a given
> model?
>
> I am using djcelery. I have a BakedModel with a FK to TaskMeta, which
> is the model that stores task status (I only care whether it is
> complete). I want to generate a query set that will return all
> instances of a given model where all deep references to and from the
> instance that are TaskMeta instances have status=SUCCESS. Currently, I
> am able to perform said operation for all relations from a given
> model, but the other way -- to a given 
> model.http://paste.pocoo.org/show/526681/

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



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

2011-12-27 Thread Russell Keith-Magee
On Wed, Dec 28, 2011 at 10:01 AM, Daniel Kaplun  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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.