I will try this out also. Thanks!

On Tue, Dec 17, 2013 at 1:57 PM, Arnold Krille <[email protected]> wrote:

> Hi,
>
> define related names in ProjectMember:
>
> class ProjectMember(models.Model):
>     project = models.ForeignKey(Project, related_name='members')
>     member = models.ForeignKey(User)
>     added_on = models.DateTimeField()
>
> The full query to get all Projects the User is either member or leader:
>
> Projects.objects.filter(
>     Q(owner=user) | Q(members__member=user)
> )
>
> Don't merge in python what you can merge in the database. One
> query in the database is (almost) always faster then two queries and
> merging in python.
>
> Have fun,
>
> Arnold
>
> Am Mon, 16 Dec 2013 09:56:27 -0800 (PST)
> schrieb antialiasis <[email protected]>:
> > Rather than fetching
> > ProjectMember.objects.filter(member=request.user), you want to fetch
> > Project.objects.filter(projectmember_set__member=request.user). That
> > will get you Project objects rather than ProjectMember objects.
> >
> > On Monday, December 16, 2013 1:47:13 PM UTC, Vibhu Rishi wrote:
> > >
> > > Hi
> > >
> > > I am not able to figure this out. I want to add objects to a query
> > > set.
> > >
> > > I have 2 models :
> > > 1. Projects which is basically a list of projects. I have a field
> > > which defines the owner of the project. e.g. :
> > >
> > > class Project(models.Model):
> > >     ...
> > >     owner = models.ForeignKey(User)
> > >
> > > 2. ProjectMember - This is a table where I add any user ( other
> > > than owner ) if they are member of the project.
> > >
> > > class ProjectMember(models.Model):
> > >     project = models.ForeignKey(Project)
> > >     member = models.ForeignKey(User)
> > >     added_on = models.DateTimeField()
> > >
> > > What I am trying to do is get a list of projects which the current
> > > user is either owner or member of. So, I have this in my view :
> > >
> > > def mylist(request):
> > >     projects = Project.objects.filter(owner=request.user)
> > >     member_of = ProjectMember.objects.filter(member = request.user)
> > >     # Now find all projects the user is a member of and add to the
> > > projects list
> > >     all_projects = projects
> > >     for m in member_of:
> > >         all_projects |= m.project
> > >     return render (request, "project/projects_mine.html",
> > > {'projects':projects})
> > >
> > >
> > > I am doing something wrong here as the line all_projects |=
> > > m.project is not working. I tried with a += also.
> > >
> > > How can I achieve what I am trying to do ?
> > >
> > > Vibhu
> > >
> > > --
> > > Simplicity is the ultimate sophistication. - Leonardo da Vinci
> > > Life is really simple, but we insist on making it complicated. -
> > > Confucius
> > >
> >
>
>


-- 
Simplicity is the ultimate sophistication. - Leonardo da Vinci
Life is really simple, but we insist on making it complicated. - Confucius

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPiONwkrt35x43uEcdZkrsdm3N1FiA%3DEFww9rkGEB2G-P5RqoA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to