List Info

Thread: Getting related data...




Getting related data...
user name
2007-12-31 18:07:50
Hi All,

I'm trying to figure out how to accomplish something using
Django's
database API, that I can easily do with SQL.  Here's my
models:

class Service(models.Model):
   name = models.CharField(max_length=50)

class Profile(models.Model):
   user = models.ForeignKey(User)
   service = models.ManyToManyField(Service)

So, I could have hundreds of Services, but I'm only
interested in
listing the Services that have been listed in any Profile. 
In regards
to the base tables, this would look like:

select * from app_profile_services;

How can I accomplish this using Django's database API?

Keith
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Getting related data...
user name
2007-12-31 20:04:31
There's probably an easier/more efficient way to do it, but
you can do

services = []
for p in Profile.objects.all():
    services += p.service_set.all()

Todd

On Dec 31, 2007 7:07 PM, ocgstyles <keith.eberlegmail.com> wrote:
>
> Hi All,
>
> I'm trying to figure out how to accomplish something
using Django's
> database API, that I can easily do with SQL.  Here's my
models:
>
> class Service(models.Model):
>    name = models.CharField(max_length=50)
>
> class Profile(models.Model):
>    user = models.ForeignKey(User)
>    service = models.ManyToManyField(Service)
>
> So, I could have hundreds of Services, but I'm only
interested in
> listing the Services that have been listed in any
Profile.  In regards
> to the base tables, this would look like:
>
> select * from app_profile_services;
>
> How can I accomplish this using Django's database API?
>
> Keith
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Getting related data...
user name
2008-01-01 08:14:40
maybe you'll use a set on the result so you don't have
duplicates,
a least i think so..

On Jan 1, 2:04 am, "Todd O'Bryan"
<toddobr...gmail.com> wrote:
> There's probably an easier/more efficient way to do it,
but you can do
>
> services = []
> for p in Profile.objects.all():
>     services += p.service_set.all()
>
> Todd
>
> On Dec 31, 2007 7:07 PM, ocgstyles <keith.ebe...gmail.com> wrote:
>
>
>
> > Hi All,
>
> > I'm trying to figure out how to accomplish
something using Django's
> > database API, that I can easily do with SQL.
 Here's my models:
>
> > class Service(models.Model):
> >    name = models.CharField(max_length=50)
>
> > class Profile(models.Model):
> >    user = models.ForeignKey(User)
> >    service = models.ManyToManyField(Service)
>
> > So, I could have hundreds of Services, but I'm
only interested in
> > listing the Services that have been listed in any
Profile.  In regards
> > to the base tables, this would look like:
>
> > select * from app_profile_services;
>
> > How can I accomplish this using Django's database
API?
>
> > Keith
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Getting related data...
user name
2008-01-01 13:42:26
Thanks Todd.

I came up with this:

def get_services():
   d = {}
   for p in Profile.objects.all():
      for s in p.services.all():
         if not d.has_key(s.slug):
            d[s.slug] = s.name
   return d

So I pass this function as extra context (service_list) to
the
template.  When I try to render the results in the template,
I can't
get it to work:

{% for service in service_list %}
  <li>{{ service_list.service }}</li>
{% endfor %}

The template documentation says it will first try a
dictionary lookup
when it encounters a dot.  Am I misunderstanding this?

Keith





On Dec 31 2007, 9:04 pm, "Todd O'Bryan"
<toddobr...gmail.com> wrote:
> There's probably an easier/more efficient way to do it,
but you can do
>
> services = []
> for p in Profile.objects.all():
>     services += p.service_set.all()
>
> Todd
>
> On Dec 31, 2007 7:07 PM, ocgstyles <keith.ebe...gmail.com> wrote:
>
>
>
> > Hi All,
>
> > I'm trying to figure out how to accomplish
something using Django's
> > database API, that I can easily do with SQL. 
Here's my models:
>
> > class Service(models.Model):
> >    name = models.CharField(max_length=50)
>
> > class Profile(models.Model):
> >    user = models.ForeignKey(User)
> >    service = models.ManyToManyField(Service)
>
> > So, I could have hundreds of Services, but I'm
only interested in
> > listing the Services that have been listed in any
Profile.  In regards
> > to the base tables, this would look like:
>
> > select * from app_profile_services;
>
> > How can I accomplish this using Django's database
API?
>
> > Keith
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Getting related data...
user name
2008-01-01 14:02:03
I just found a nice blog post: http://push.cx/2007/django-template-tag-for-diction
ary-access

That seems to work nicely.  =)

On Jan 1, 2:42 pm, ocgstyles <keith.ebe...gmail.com> wrote:
> Thanks Todd.
>
> I came up with this:
>
> def get_services():
>    d = {}
>    for p in Profile.objects.all():
>       for s in p.services.all():
>          if not d.has_key(s.slug):
>             d[s.slug] = s.name
>    return d
>
> So I pass this function as extra context (service_list)
to the
> template.  When I try to render the results in the
template, I can't
> get it to work:
>
> {% for service in service_list %}
>   <li>{{ service_list.service }}</li>
> {% endfor %}
>
> The template documentation says it will first try a
dictionary lookup
> when it encounters a dot.  Am I misunderstanding this?
>
> Keith
>
> On Dec 31 2007, 9:04 pm, "Todd O'Bryan"
<toddobr...gmail.com> wrote:
>
> > There's probably an easier/more efficient way to
do it, but you can do
>
> > services = []
> > for p in Profile.objects.all():
> >     services += p.service_set.all()
>
> > Todd
>
> > On Dec 31, 2007 7:07 PM, ocgstyles
<keith.ebe...gmail.com> wrote:
>
> > > Hi All,
>
> > > I'm trying to figure out how to accomplish
something using Django's
> > > database API, that I can easily do with SQL. 
Here's my models:
>
> > > class Service(models.Model):
> > >    name = models.CharField(max_length=50)
>
> > > class Profile(models.Model):
> > >    user = models.ForeignKey(User)
> > >    service = models.ManyToManyField(Service)
>
> > > So, I could have hundreds of Services, but
I'm only interested in
> > > listing the Services that have been listed in
any Profile.  In regards
> > > to the base tables, this would look like:
>
> > > select * from app_profile_services;
>
> > > How can I accomplish this using Django's
database API?
>
> > > Keith
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )