how to get all objects related to a particular user using django ContentType Framework

338 views
Skip to first unread message

Sarfraz ahmad

unread,
Jan 23, 2013, 9:00:38 AM1/23/13
to django...@googlegroups.com
hello friends
                  i have a project with 7 applications installed in it and i want to get all the objects related to a particular user from all the applications of ma project......... please tell me how can i get all these objects using ContentType framework


thank you all

Pankaj Singh

unread,
Jan 23, 2013, 9:12:05 AM1/23/13
to django...@googlegroups.com
Hey Sarfraz,

You can use any of following methods:

User._meta.get_all_related_m2m_objects_with_model()
User._meta.get_all_related_objects()
User._meta.get_all_related_many_to_many_objects()
User._meta.get_all_related_objects_with_model()

get_all_related_objects() is the one I guess you may want to use in your case.

Pankaj Singh
http://about.me/psjinx
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/tKRQQKC06BsJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Pankaj Singh

unread,
Jan 23, 2013, 9:30:45 AM1/23/13
to django...@googlegroups.com
Hey Sarfraz,

If you have an user object, then you can get all related objects using
following code

user = User.objects.get(username="psjinx")

related_links = [rel.get_accessor_name() for rel in
user._meta.get_all_related_objects()]

## above code will give a list of attribute names for each related
object to an user
## e.g. ['logentry_set', 'api_key', 'userprofile_set',
'recipient_set', 'customer']

Now you can iterate over this list

for link in related_links:
objects = getattr(user, link).all()
for object in objects:
## do something with object

FYI, getattr(user, link) is manager for that relate object.


Pankaj Singh
http://about.me/psjinx

Sarfraz ahmad

unread,
Jan 23, 2013, 9:59:50 AM1/23/13
to django...@googlegroups.com
thanx buddy bt i wish to do it in a manner that a model which has a foreign key to ContentType, when i make a query on this model it returns all the objects from various apps related to current logged in user

Pankaj Singh

unread,
Jan 23, 2013, 10:08:18 AM1/23/13
to django...@googlegroups.com
So, you have a custom model like following

class MyModel(models.Model):
...
content_type = models.ForeignKey(ContentType)
...

And you want to run a query on MyModel which should return objects
from various apps related to currently logged in User.

Is this what you want to achieve?

Pankaj Singh
http://about.me/psjinx

Sarfraz ahmad

unread,
Jan 23, 2013, 10:23:12 AM1/23/13
to django...@googlegroups.com
i have the same model having one foreignkey to User and second to the ContentType

class A11M1_user_itmes(models.Model):
    A11M1F1_user=models.ForeignKey(User)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
this is the code of ma model...... using this code i wanna get all objects related to current logged in user

Pankaj Singh

unread,
Jan 23, 2013, 10:31:26 AM1/23/13
to django...@googlegroups.com
If you want to get all `A11M1_user_items` objects then following query
should work


--
Pankaj Singh
http://about.me/psjinx

Pankaj Singh

unread,
Jan 23, 2013, 10:41:09 AM1/23/13
to django...@googlegroups.com
Sorry for last reply. I sent uncompleted email by mistake, while
looking at other laptop.

If you want to get all `A11M1_user_itmes ` objects then following
query should work

objects = request.user. a11m1_user_itmes_set.all()

You can use `content_object` attribute on each object in objects list
to get original object.

I used similar approach for creating a new feed similar to facebook.
Please have a look at related stackoverflow question,
http://stackoverflow.com/questions/2128886/django-way-for-building-a-news-feed-status-update-activity-stream.

--
Pankaj Singh
http://about.me/psjinx


Sarfraz ahmad

unread,
Jan 23, 2013, 10:41:41 AM1/23/13
to django...@googlegroups.com
i dont found any query related to this model bro........

Pankaj Singh

unread,
Jan 23, 2013, 10:58:14 AM1/23/13
to django...@googlegroups.com
items = request.user. a11m1_user_itmes_set.all()

is equivalent to

items = A11M1_user_itmes.objects.filter(user=request.user)

`items` will contain all items A11M1_user_itmes related to currently
logged in user.

Now, if you want to get `content_object` for a particular item, do
something like following

i = items[0]

`i.content_object` will refer to original object used.

Please go through official documentation for GenericForeignKey once more.

Links:
1. https://docs.djangoproject.com/en/1.4/ref/contrib/contenttypes/#django.contrib.contenttypes.generic.GenericForeignKey

--
Pankaj Singh
http://about.me/psjinx


Rafael E. Ferrero

unread,
Jan 23, 2013, 11:10:01 AM1/23/13
to django...@googlegroups.com
Sorry, but select_related() dont work for you??

2013/1/23 Pankaj Singh <ps....@gmail.com>



--
Rafael E. Ferrero
Claro: (03562) 15514856

Sarfraz ahmad

unread,
Jan 23, 2013, 12:01:30 PM1/23/13
to django...@googlegroups.com
items = request.user. a11m1_user_itmes_set.all() works correctly
thank you buddies
Reply all
Reply to author
Forward
0 new messages