Wednesday, January 23, 2013

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

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


On Wed, Jan 23, 2013 at 3:29 PM, Sarfraz ahmad <sarfrazdjango@gmail.com> wrote:
> 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
>
>
>
>
> On Wed, Jan 23, 2013 at 3:00 PM, Pankaj Singh <ps.jinx@gmail.com> wrote:
>>
>> 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
>>
>>
>> On Wed, Jan 23, 2013 at 2:42 PM, Pankaj Singh <ps.jinx@gmail.com> wrote:
>> > 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
>> >
>> >
>> > On Wed, Jan 23, 2013 at 2:30 PM, Sarfraz ahmad <sarfrazdjango@gmail.com>
>> > wrote:
>> >> 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
>> >>
>> >> --
>> >> 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-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.
>>
>> --
>> 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.
>>
>
> --
> 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.

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate