|
List Info
Thread: Re: Need some help with a query if you can :)
|
|
| Re: Need some help with a query if you
can :) |

|
2008-02-23 05:48:21 |
Hmmm ok, can't get this to print anything. When trying to
print the
threads array it does this:
[, []]
[, []]
So, it is looping through my friends properly, but it not
getting
their threads. To be honest, ideally I just want to print a
list of
all the threads my friends are in, and not a list of all my
friends
plus their threads. I.E:
Thread: ABC
Friend 1
Thread: XYZ
Friend 1, Friend 2, Friend 3
And not:
Friend 1
Threads: ABC, XYZ
Friend 2
Threads: XYZ
Here is the latest code:
Model:
-----
class Thread(models.Model):
title = models.CharField(maxlength=200)
users = models.ManyToManyField(User,
related_name='threads')
def __str__(self):
return self.title
View:
-----
# get profile
profile = get_object_or_404(UserProfile,
user__id__exact=request.user.id)
# get friends for user
friends = profile.friends.all()
# get threads
threads = []
for friend in friends:
threads.append([friend, friend.threads.all()])
return render_to_response('people/threads.html', {
"thread_list" :
threads }, context_instance=RequestContext(request))
Template:
-----
{% for thread in thread_list %}
<h2>{{ thread.title }}</h2>
<p>{{ thread.friends|join:", "
}}</p>
{% endfor %}
On Feb 22, 11:09 pm, Julien <jpha... gmail.com> wrote:
> If you look at my code, I put
"related_name='threads'"
> If you don't specify that, then it is automatically set
to
> "thread_set" (see here:http://django.tomsk.ru/documentation/models/reverse_l
ookup/)
>
> Also, in your view, do this instead:
> return render_to_response('people/threads.html', {
"friends" :
> friends}, context_instance=RequestContext(request))
>
> Your template should then be:
> {% for friend in friends %}
> <h2>{}</h2>
> <p>{{ friend.thread_set|join:", "
}}</p>
> {% endfor %}
>
> All that is if you want to display all the threads for
each friend.
>
> On Feb 23, 9:55 am, Darthmahon <cpma... gmail.com> wrote:
>
> > Ok, just tried this:
>
> > View:
> > -----
> > # get threads for friends
> > threads = []
> > for friend in friends:
> > threads.append([friend,
friend.thread_set.all()])
> > -----
>
> > This appears to print something back, but it looks
like it is just
> > printing my friends.
>
> > Not sure how to loop through this result properly
in the template. At
> > the moment I am doing this - but the template is
probably completely
> > wrong in terms of printing my friends, within each
thread:
>
> > -----
> > View:
> > -----
> > # get threads for friends
> > threads = []
> > for friend in friends:
> > threads.append([friend,
friend.thread_set.all()])
>
> > return render_to_response('people/threads.html', {
"thread_list" :
> > threads },
context_instance=RequestContext(request))
> > -----
>
> > -----
> > Template:
> > -----
> > {% for thread in thread_list %}
>
> > <h2>{{ thread.title }}</h2>
> > <p>{{ thread.friends|join:", "
}}</p>
>
> > {% endfor %}
> > -----
>
> > On Feb 22, 10:44 pm, Darthmahon <cpma... gmail.com> wrote:
>
> > > Hi Julien,
>
> > > Just gave that a go, getting an error:
>
> > > 'User' object has no attribute 'threads'
>
> > > I'm guessing that because I am referring to
the User model, which has
> > > no direct relation to the Thread module, this
won't work?
>
> > > Julien wrote:
> > > > Hi, how about this? (I haven't tested it
myself)
>
> > > > Models:
> > > > -------
>
> > > > class UserProfile(models.Model):
> > > > user = models.ForeignKey(User,
unique=True)
> > > > friends =
models.ManyToManyField(User, blank=True,
> > > > related_name='friends')
>
> > > > class Thread(models.Model):
> > > > title =
models.CharField(maxlength=200)
> > > > users =
models.ManyToManyField(User, related_name='threads')
>
> > > > View:
> > > > -----
>
> > > > # get profile
> > > > profile =
get_object_or_404(UserProfile,
> > > > user__id__exact=request.user.id)
>
> > > > # get friends for user
> > > > friends = profile.friends.all()
>
> > > > # get threads for friends
> > > > threads = []
> > > > for friend in friends:
> > > > thread.append([friend,
friend.threads.all()])
>
> > > > On Feb 23, 8:34 am, Darthmahon
<cpma... gmail.com> wrote:
> > > > > Hey,
>
> > > > > I have three models but I can only
handle the relationship between two
> > > > > at any one time.
>
> > > > > Basically I have a page that needs
to show a threads the current
> > > > > user's friends are currently
participating in. I.E. something like
> > > > > this:
>
> > > > > Thread: ABC
> > > > > Participating: Friend 1
>
> > > > > Thread: XYZ
> > > > > Participating: Friend 1, Friend 2,
Friend 3
>
> > > > > Here are my models (both of which
refer to the default Auth model):
>
> > > > > ----------------------------------
> > > > > File: /people/models.py
> > > > > ----------------------------------
>
> > > > > class UserProfile(models.Model):
>
> > > > > user =
models.ForeignKey(User, unique=True)
> > > > > friends =
models.ManyToManyField(User, blank=True,
> > > > > related_name='friend_set')
>
> > > > > ----------------------------------
> > > > > File: /thread/models.py
> > > > > ----------------------------------
>
> > > > > class Thread(models.Model):
>
> > > > > title =
models.CharField(maxlength=200)
> > > > > users =
models.ManyToManyField(User, related_name='users')
>
> > > > > ----------------------------------
>
> > > > > Now here is my view file:
>
> > > > > ----------------------------------
> > > > > File: /people/views.py
> > > > > ----------------------------------
>
> > > > > def index(request):
>
> > > > > # get profile
> > > > > profile =
get_object_or_404(UserProfile,
> > > > > user__id__exact=request.user.id)
>
> > > > > # get friends for user
> > > > > friends =
profile.friends.all()
>
> > > > > # get threads for friends
> > > > > ???????????
>
> > > > > ----------------------------------
>
> > > > > As you can see, I don't know what
to do now I need to get the threads
> > > > > for each individual user. I don't
need to group them in a special way,
> > > > > I just want to print the threads my
friends are currently
> > > > > participating in an alphabetical
list.
>
> > > > > I did wonder if I should just edit
the UserProfile model like this?
>
> > > > > ----------------------------------
> > > > > File: /people/models.py
> > > > > ----------------------------------
>
> > > > > class UserProfile(models.Model):
>
> > > > > user =
models.ForeignKey(User, unique=True)
> > > > > friends =
models.ManyToManyField(User, blank=True,
> > > > > related_name='friend_set')
> > > > > threads =
models.ManyToManyField(Thread, blank=True,
> > > > > related_name='thread_set')
>
> > > > > ----------------------------------
>
> > > > > Any ideas on how to do this? I'm
guessing it's probably fairly basic
> > > > > but I can't think how I would get
around this at the moment (it's been
> > > > > a long week!) ;)
>
> > > > > Cheers,
> > > > > Chris
>
> > On Feb 22, 10:44 pm, Darthmahon <cpma... gmail.com> wrote:
>
> > > Hi Julien,
>
> > > Just gave that a go, getting an error:
>
> > > 'User' object has no attribute 'threads'
>
> > > I'm guessing that because I am referring to
the User model, which has
> > > no direct relation to the Thread module, this
won't work?
>
> > > Julien wrote:
> > > > Hi, how about this? (I haven't tested it
myself)
>
> > > > Models:
> > > > -------
>
> > > > class UserProfile(models.Model):
> > > > user = models.ForeignKey(User,
unique=True)
> > > > friends =
models.ManyToManyField(User, blank=True,
> > > > related_name='friends')
>
> > > > class Thread(models.Model):
> > > > title =
models.CharField(maxlength=200)
> > > > users =
models.ManyToManyField(User, related_name='threads')
>
> > > > View:
> > > > -----
>
> > > > # get profile
> > > > profile =
get_object_or_404(UserProfile,
> > > > user__id__exact=request.user.id)
>
> > > > # get friends for user
> > > > friends = profile.friends.all()
>
> > > > # get threads for friends
> > > > threads = []
> > > > for friend in friends:
> > > > thread.append([friend,
friend.threads.all()])
>
> > > > On Feb 23, 8:34 am, Darthmahon
<cpma... gmail.com> wrote:
> > > > > Hey,
>
> > > > > I have three models but I can only
handle the relationship between two
> > > > > at any one time.
>
> > > > > Basically I have a page that needs
to show a threads the current
> > > > > user's friends are currently
participating in. I.E. something like
> > > > > this:
>
> > > > > Thread: ABC
> > > > > Participating: Friend 1
>
> > > > > Thread: XYZ
> > > > > Participating: Friend 1, Friend 2,
Friend 3
>
> > > > > Here are my models (both of which
refer to the default Auth model):
>
> > > > > ----------------------------------
> > > > > File: /people/models.py
> > > > > ----------------------------------
>
> > > > > class UserProfile(models.Model):
>
> > > > > user =
models.ForeignKey(User, unique=True)
> > > > > friends =
models.ManyToManyField(User, blank=True,
> > > > > related_name='friend_set')
>
> > > > > ----------------------------------
> > > > > File: /thread/models.py
> > > > > ----------------------------------
>
> > > > > class Thread(models.Model):
>
> > > > > title =
models.CharField(maxlength=200)
> > > > > users =
models.ManyToManyField(User, related_name='users')
>
> > > > > ----------------------------------
>
> > > > > Now here is my view file:
>
> > > > > ----------------------------------
> > > > > File: /people/views.py
> > > > > ----------------------------------
>
> > > > > def index(request):
>
> > > > > # get profile
> > > > > profile =
get_object_or_404(UserProfile,
> > > > > user__id__exact=request.user.id)
>
> > > > > # get friends for user
> > > > > friends =
profile.friends.all()
>
> > > > > # get threads for friends
> > > > > ???????????
>
> > > > > ----------------------------------
>
> > > > > As you can see, I don't know what
to do now I need to get the threads
> > > > > for each individual user. I don't
need to group them in a special way,
> > > > > I just want to print the threads my
friends are currently
> > > > > participating in an alphabetical
list.
>
> > > > > I did wonder if I should just edit
the UserProfile model like this?
>
> > > > > ----------------------------------
> > > > > File: /people/models.py
> > > > > ----------------------------------
>
> > > > > class UserProfile(models.Model):
>
> > > > > user =
models.ForeignKey(User, unique=True)
> > > > > friends =
models.ManyToManyField(User, blank=True,
> > > > > related_name='friend_set')
> > > > > threads =
models.ManyToManyField(Thread, blank=True,
> > > > > related_name='thread_set')
>
> > > > > ----------------------------------
>
> > > > > Any ideas on how to do this? I'm
guessing it's probably fairly basic
> > > > > but I can't think how I would get
around this at the moment (it's been
> > > > > a long week!) ;)
>
> > > > > Cheers,
> > > > > Chris
--~--~---------~--~----~------------~-------~--~----~
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 htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Need some help with a query if you
can :) |

|
2008-02-24 04:19:07 |
Hi Guys,
Just wondered if anyone else could help out on this one? To
me it
doesn't seem like it's a difficult thing to do, I just can't
get it to
work :(
Cheers,
Chris
On Feb 23, 11:48 am, Darthmahon <cpma... gmail.com> wrote:
> Hmmm ok, can't get this to print anything. When trying
to print the
> threads array it does this:
>
> [, []]
> [, []]
>
> So, it is looping through my friends properly, but it
not getting
> their threads. To be honest, ideally I just want to
print a list of
> all the threads my friends are in, and not a list of
all my friends
> plus their threads. I.E:
>
> Thread: ABC
> Friend 1
>
> Thread: XYZ
> Friend 1, Friend 2, Friend 3
>
> And not:
>
> Friend 1
> Threads: ABC, XYZ
>
> Friend 2
> Threads: XYZ
>
> Here is the latest code:
>
> Model:
> -----
>
> class Thread(models.Model):
>
> title =
models.CharField(maxlength=200)
> users = models.ManyToManyField(User,
related_name='threads')
>
> def __str__(self):
> return self.title
>
> View:
> -----
>
> # get profile
> profile = get_object_or_404(UserProfile,
> user__id__exact=request.user.id)
>
> # get friends for user
> friends = profile.friends.all()
>
> # get threads
> threads = []
> for friend in friends:
> threads.append([friend,
friend.threads.all()])
>
> return
render_to_response('people/threads.html', {
"thread_list" :
> threads }, context_instance=RequestContext(request))
>
> Template:
> -----
>
> {% for thread in thread_list %}
> <h2>{{ thread.title
}}</h2>
> <p>{{ thread.friends|join:",
" }}</p>
> {% endfor %}
>
> On Feb 22, 11:09 pm, Julien <jpha... gmail.com> wrote:
>
> > If you look at my code, I put
"related_name='threads'"
> > If you don't specify that, then it is
automatically set to
> > "thread_set" (see here:http://django.tomsk.ru/documentation/models/reverse_l
ookup/)
>
> > Also, in your view, do this instead:
> > return render_to_response('people/threads.html', {
"friends" :
> > friends},
context_instance=RequestContext(request))
>
> > Your template should then be:
> > {% for friend in friends %}
> > <h2>{}</h2>
> > <p>{{ friend.thread_set|join:",
" }}</p>
> > {% endfor %}
>
> > All that is if you want to display all the threads
for each friend.
>
> > On Feb 23, 9:55 am, Darthmahon <cpma... gmail.com> wrote:
>
> > > Ok, just tried this:
>
> > > View:
> > > -----
> > > # get threads for friends
> > > threads = []
> > > for friend in friends:
> > > threads.append([friend,
friend.thread_set.all()])
> > > -----
>
> > > This appears to print something back, but it
looks like it is just
> > > printing my friends.
>
> > > Not sure how to loop through this result
properly in the template. At
> > > the moment I am doing this - but the template
is probably completely
> > > wrong in terms of printing my friends, within
each thread:
>
> > > -----
> > > View:
> > > -----
> > > # get threads for friends
> > > threads = []
> > > for friend in friends:
> > > threads.append([friend,
friend.thread_set.all()])
>
> > > return
render_to_response('people/threads.html', {
"thread_list" :
> > > threads },
context_instance=RequestContext(request))
> > > -----
>
> > > -----
> > > Template:
> > > -----
> > > {% for thread in thread_list %}
>
> > > <h2>{{ thread.title }}</h2>
> > > <p>{{ thread.friends|join:",
" }}</p>
>
> > > {% endfor %}
> > > -----
>
> > > On Feb 22, 10:44 pm, Darthmahon
<cpma... gmail.com> wrote:
>
> > > > Hi Julien,
>
> > > > Just gave that a go, getting an error:
>
> > > > 'User' object has no attribute
'threads'
>
> > > > I'm guessing that because I am referring
to the User model, which has
> > > > no direct relation to the Thread module,
this won't work?
>
> > > > Julien wrote:
> > > > > Hi, how about this? (I haven't
tested it myself)
>
> > > > > Models:
> > > > > -------
>
> > > > > class UserProfile(models.Model):
> > > > > user = models.ForeignKey(User,
unique=True)
> > > > > friends =
models.ManyToManyField(User, blank=True,
> > > > > related_name='friends')
>
> > > > > class Thread(models.Model):
> > > > > title =
models.CharField(maxlength=200)
> > > > > users =
models.ManyToManyField(User, related_name='threads')
>
> > > > > View:
> > > > > -----
>
> > > > > # get profile
> > > > > profile =
get_object_or_404(UserProfile,
> > > > > user__id__exact=request.user.id)
>
> > > > > # get friends for user
> > > > > friends = profile.friends.all()
>
> > > > > # get threads for friends
> > > > > threads = []
> > > > > for friend in friends:
> > > > > thread.append([friend,
friend.threads.all()])
>
> > > > > On Feb 23, 8:34 am, Darthmahon
<cpma... gmail.com> wrote:
> > > > > > Hey,
>
> > > > > > I have three models but I can
only handle the relationship between two
> > > > > > at any one time.
>
> > > > > > Basically I have a page that
needs to show a threads the current
> > > > > > user's friends are currently
participating in. I.E. something like
> > > > > > this:
>
> > > > > > Thread: ABC
> > > > > > Participating: Friend 1
>
> > > > > > Thread: XYZ
> > > > > > Participating: Friend 1,
Friend 2, Friend 3
>
> > > > > > Here are my models (both of
which refer to the default Auth model):
>
> > > > > >
----------------------------------
> > > > > > File: /people/models.py
> > > > > >
----------------------------------
>
> > > > > > class
UserProfile(models.Model):
>
> > > > > > user =
models.ForeignKey(User, unique=True)
> > > > > > friends =
models.ManyToManyField(User, blank=True,
> > > > > > related_name='friend_set')
>
> > > > > >
----------------------------------
> > > > > > File: /thread/models.py
> > > > > >
----------------------------------
>
> > > > > > class Thread(models.Model):
>
> > > > > > title =
models.CharField(maxlength=200)
> > > > > > users =
models.ManyToManyField(User, related_name='users')
>
> > > > > >
----------------------------------
>
> > > > > > Now here is my view file:
>
> > > > > >
----------------------------------
> > > > > > File: /people/views.py
> > > > > >
----------------------------------
>
> > > > > > def index(request):
>
> > > > > > # get profile
> > > > > > profile =
get_object_or_404(UserProfile,
> > > > > >
user__id__exact=request.user.id)
>
> > > > > > # get friends for
user
> > > > > > friends =
profile.friends.all()
>
> > > > > > # get threads for
friends
> > > > > > ???????????
>
> > > > > >
----------------------------------
>
> > > > > > As you can see, I don't know
what to do now I need to get the threads
> > > > > > for each individual user. I
don't need to group them in a special way,
> > > > > > I just want to print the
threads my friends are currently
> > > > > > participating in an
alphabetical list.
>
> > > > > > I did wonder if I should just
edit the UserProfile model like this?
>
> > > > > >
----------------------------------
> > > > > > File: /people/models.py
> > > > > >
----------------------------------
>
> > > > > > class
UserProfile(models.Model):
>
> > > > > > user =
models.ForeignKey(User, unique=True)
> > > > > > friends =
models.ManyToManyField(User, blank=True,
> > > > > > related_name='friend_set')
> > > > > > threads =
models.ManyToManyField(Thread, blank=True,
> > > > > > related_name='thread_set')
>
> > > > > >
----------------------------------
>
> > > > > > Any ideas on how to do this?
I'm guessing it's probably fairly basic
> > > > > > but I can't think how I would
get around this at the moment (it's been
> > > > > > a long week!) ;)
>
> > > > > > Cheers,
> > > > > > Chris
>
> > > On Feb 22, 10:44 pm, Darthmahon
<cpma... gmail.com> wrote:
>
> > > > Hi Julien,
>
> > > > Just gave that a go, getting an error:
>
> > > > 'User' object has no attribute
'threads'
>
> > > > I'm guessing that because I am referring
to the User model, which has
> > > > no direct relation to the Thread module,
this won't work?
>
> > > > Julien wrote:
> > > > > Hi, how about this? (I haven't
tested it myself)
>
> > > > > Models:
> > > > > -------
>
> > > > > class UserProfile(models.Model):
> > > > > user = models.ForeignKey(User,
unique=True)
> > > > > friends =
models.ManyToManyField(User, blank=True,
> > > > > related_name='friends')
>
> > > > > class Thread(models.Model):
> > > > > title =
models.CharField(maxlength=200)
> > > > > users =
models.ManyToManyField(User, related_name='threads')
>
> > > > > View:
> > > > > -----
>
> > > > > # get profile
> > > > > profile =
get_object_or_404(UserProfile,
> > > > > user__id__exact=request.user.id)
>
> > > > > # get friends for user
> > > > > friends = profile.friends.all()
>
> > > > > # get threads for friends
> > > > > threads = []
> > > > > for friend in friends:
> > > > > thread.append([friend,
friend.threads.all()])
>
> > > > > On Feb 23, 8:34 am, Darthmahon
<cpma... gmail.com> wrote:
> > > > > > Hey,
>
> > > > > > I have three models but I can
only handle the relationship between two
> > > > > > at any one time.
>
> > > > > > Basically I have a page that
needs to show a threads the current
> > > > > > user's friends are currently
participating in. I.E. something like
> > > > > > this:
>
> > > > > > Thread: ABC
> > > > > > Participating: Friend 1
>
> > > > > > Thread: XYZ
> > > > > > Participating: Friend 1,
Friend 2, Friend 3
>
> > > > > > Here are my models (both of
which refer to the default Auth model):
>
> > > > > >
----------------------------------
> > > > > > File: /people/models.py
> > > > > >
----------------------------------
>
> > > > > > class
UserProfile(models.Model):
>
> > > > > > user =
models.ForeignKey(User, unique=True)
> > > > > > friends =
models.ManyToManyField(User, blank=True,
> > > > > > related_name='friend_set')
>
> > > > > >
----------------------------------
> > > > > > File: /thread/models.py
> > > > > >
----------------------------------
>
> > > > > > class Thread(models.Model):
>
> > > > > > title =
models.CharField(maxlength=200)
> > > > > > users =
models.ManyToManyField(User, related_name='users')
>
> > > > > >
----------------------------------
>
> > > > > > Now here is my view file:
>
> > > > > >
----------------------------------
> > > > > > File:
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
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 htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
[1-2]
|
|