Re: Django and a basic SQL join

2009-05-08 Thread Malcolm Tredinnick

On Wed, 2009-05-06 at 13:14 -0700, jrs_66 wrote:
> Thanks!  This is a great starting point.  My real queryset requires me
> to go a few joins deeper, however.  I will try nesting loops to get
> there, but this seems frightfully like querying in a loop (maybe I'm
> wrong, I'll check the end query to find out).  It also seems like a
> frightening amount of code to write to to produce a query which
> ultimately looks like this in SQL.
> 
> #SELECT containers.* FROM containers\
> #INNER JOIN containers_and_categories ON\
> #containers.id = containers_and_categories.container_id\
> #INNER JOIN flattened_category ON\
> #containers_and_categories.category_id =
> flattened_category.member_of_category_id\
> #WHERE flattened_category.member_of_category_id = %s",
> [request.POST["cat-id"]])

Okay, so your question is to restrict the results you're getting back.
That isn't quite what you asked. You mentioned what didn't work and
people were trying to explain how to access related objects given an
instance object, which was a reasonable guess. However, it wasn't too
easy to work out what you were trying to do, so, for next time, a bit of
an example of "here's the information I want to get at" would probably
help.

You haven't specified what the Container model looks like, but based on
the query you've written, I'd gues you're after


Container.objects.filter(category__flattenedcategory__id=request.POST["cat-id"])

The documentation for traversing relations inside filters is here:
http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships

Querysets are iteratively (each method call being an iteration)
restricted sets of results. Predominantly filter() and exclude() being
used to restrict the results. What you were trying to do originally,
accessing an attribute, cannot work, because it is an attribute access
on the Queryset -- which doesn't have that kind of attribute -- not on a
Category model instance.

Regards,
Malcolm


--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Django and a basic SQL join

2009-05-06 Thread jrs_66

Phil,

Thanks for the pointers.  I guess my thinking on this is that if
someone feels a question is too trite to warrant and answer, they
shouldn't answer.  I don't think this group is called 'advanced django
users', thus I don't really feel bad for posting 'newbie' questions.
I appreciate all answers, it's just the steady stream (not only to me)
of dismissive 'I'll make 'em feel lousy' comments which bother me.

On May 6, 2:45 pm, Phil Mocek 
wrote:
> On Wed, May 06, 2009 at 10:17:29AM -0700, jrs_66 wrote:
> > This is definitely the most angry forum I've ever seen... the
> > kicker is that the anger is almost always coming from the people
> > associated with the django project... hmmm..
>
> In your previous thread, someone from the Django project helpfully
> offered to assist you, but you ignored every question he asked in
> his effort to better understand your question -- even after I
> pointed this out in case you had done it by mistake, as tends to
> happen when people mix posting styles, as you continue to do
> despite notification that it is nearly-universally considered bad
> etiquette to do so.
>
> 
>
> After people took the time to discuss your problem with you, you
> dropped the conversation.  It has been five days since then.
> Consider what effect this might have on other people's willingness
> to take the time to help you.
>
> I'm not angry, and I don't intend to be mean, I'm just telling you
> how it is.  I can't give you a pat on the back or whatever else
> might soften things for you over e-mail.  I believe you
> misperceive this anger you describe, although when it seems that
> you are wasting other people's time, it tends to make them angry,
> so you might have detected some anger.  The solution is not to
> complain about perceived anger then, "take your ball and leave,"
> but to consider what you're doing that might be causing the
> trouble, and stop doing it.
>
> When people suggest that you Read The Fine Manual, it's usually an
> indication that they believe you are being lazy and asking for
> help when you could simply read the documentation -- that which
> others have generously provided for you -- and help yourself.
> They themselves probably did this.
>
> As a general reference on what you should do before posting a
> question to a technical forum such as this list, you may want to
> take a look at
> .  In
> order to make it clear that you *have* already read the
> documentation, simply state so in your message and explain what
> you have learned, what confuses you, and what else you want to
> know.  Teaching you how to find answers should be much more useful
> to you than simply answering your questions, so you might consider
> being grateful to those who did so instead of simply ignoring you.
>
> You might also want to take a look at:
> 
>
> The document to which I have linked is not authoritative, and is
> definitely not filled with ideas that are accepted on this
> particular list, but I've found that it makes a lot of sense and
> helps newbies to understand why things are they way they are in
> many technical forums.  I believe you'd be well off to read it.
> I'm certainly no authority, but I honestly believe that my
> responses to posts like yours free up people who know Django well
> to help solve more interesting problems than tutoring newbies on
> mailing list etiquette.
>
> Good luck.
>
> --
> Phil Mocek
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Django and a basic SQL join

2009-05-06 Thread jrs_66

Thanks!  This is a great starting point.  My real queryset requires me
to go a few joins deeper, however.  I will try nesting loops to get
there, but this seems frightfully like querying in a loop (maybe I'm
wrong, I'll check the end query to find out).  It also seems like a
frightening amount of code to write to to produce a query which
ultimately looks like this in SQL...

#SELECT containers.* FROM containers\
#INNER JOIN containers_and_categories ON\
#containers.id = containers_and_categories.container_id\
#INNER JOIN flattened_category ON\
#containers_and_categories.category_id =
flattened_category.member_of_category_id\
#WHERE flattened_category.member_of_category_id = %s",
[request.POST["cat-id"]])

I'm pondering switching to 
http://blog.doughellmann.com/2007/12/using-raw-sql-in-django.html
for much of my querying (other than very basic joins, which work well
in django)

On May 6, 2:46 pm, Alex Koshelev  wrote:
> On Wed, May 6, 2009 at 8:48 PM, jrs_66  wrote:
>
> > Hi,
>
> > I have 2 models...
>
> [skip]
>
> > e = FlattenedCategory.objects.select_related('category').filter
> > (member_of_category=15)
>
> > which works... This, however, doesn't
>
> > e = e.category
>
> > How do I access the related records from the Category model?
>
> This this QuerySet it is simple:
>
>      categories = [fcategory.category for fcategory in e]
>
> or fetch only categories right away:
>
>      categories =
> Category.objects.filter(flattenedcategory__member_of_category=15).distinct()
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Django and a basic SQL join

2009-05-06 Thread Alex Koshelev

On Wed, May 6, 2009 at 8:48 PM, jrs_66  wrote:
>
> Hi,
>
> I have 2 models...
>
[skip]
>
> e = FlattenedCategory.objects.select_related('category').filter
> (member_of_category=15)
>
> which works... This, however, doesn't
>
> e = e.category
>
> How do I access the related records from the Category model?

This this QuerySet it is simple:

 categories = [fcategory.category for fcategory in e]

or fetch only categories right away:

 categories =
Category.objects.filter(flattenedcategory__member_of_category=15).distinct()

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Django and a basic SQL join

2009-05-06 Thread Phil Mocek

On Wed, May 06, 2009 at 10:17:29AM -0700, jrs_66 wrote:
> This is definitely the most angry forum I've ever seen... the
> kicker is that the anger is almost always coming from the people
> associated with the django project... hmmm..

In your previous thread, someone from the Django project helpfully
offered to assist you, but you ignored every question he asked in
his effort to better understand your question -- even after I
pointed this out in case you had done it by mistake, as tends to
happen when people mix posting styles, as you continue to do
despite notification that it is nearly-universally considered bad
etiquette to do so.



After people took the time to discuss your problem with you, you
dropped the conversation.  It has been five days since then.
Consider what effect this might have on other people's willingness
to take the time to help you.

I'm not angry, and I don't intend to be mean, I'm just telling you
how it is.  I can't give you a pat on the back or whatever else
might soften things for you over e-mail.  I believe you
misperceive this anger you describe, although when it seems that
you are wasting other people's time, it tends to make them angry,
so you might have detected some anger.  The solution is not to
complain about perceived anger then, "take your ball and leave,"
but to consider what you're doing that might be causing the
trouble, and stop doing it.

When people suggest that you Read The Fine Manual, it's usually an
indication that they believe you are being lazy and asking for
help when you could simply read the documentation -- that which
others have generously provided for you -- and help yourself.
They themselves probably did this.

As a general reference on what you should do before posting a
question to a technical forum such as this list, you may want to
take a look at
.  In
order to make it clear that you *have* already read the
documentation, simply state so in your message and explain what
you have learned, what confuses you, and what else you want to
know.  Teaching you how to find answers should be much more useful
to you than simply answering your questions, so you might consider
being grateful to those who did so instead of simply ignoring you.

You might also want to take a look at:


The document to which I have linked is not authoritative, and is
definitely not filled with ideas that are accepted on this
particular list, but I've found that it makes a lot of sense and
helps newbies to understand why things are they way they are in
many technical forums.  I believe you'd be well off to read it.
I'm certainly no authority, but I honestly believe that my
responses to posts like yours free up people who know Django well
to help solve more interesting problems than tutoring newbies on
mailing list etiquette.

Good luck.

-- 
Phil Mocek

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Django and a basic SQL join

2009-05-06 Thread Clément Nodet

>
> In your case e is a QuerySet, with multiple FlattenedCategory objects.
>
> So the proper code would be to loop through them:
>
> {{{
> for fc in e:
>     fc.category_set
> }}}
>

Indeed,
but category being a ForeignKey field,

{{{
for fc in e:
  fc.category
}}}

will work here.

--
Clément

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Django and a basic SQL join

2009-05-06 Thread George Song

In your case e is a QuerySet, with multiple FlattenedCategory objects.

So the proper code would be to loop through them:

{{{
for fc in e:
 fc.category_set
}}}

On 5/6/2009 10:17 AM, jrs_66 wrote:
> No...   'QuerySet' object has no
> attribute 'category_set'
> 
> George,
> 
> I have read the docs... MANY times... from the docs...
> 
> 'Django also creates API accessors for the "other" side of the
> relationship -- the link from the related model to the model that
> defines the relationship. For example, a Blog object b has access to a
> list of all related Entry objects via the entry_set attribute:
> b.entry_set.all().'
> 
> If you would look at my model, you will see that I'm not attempting to
> join 'backwords'... forwards, my man... please review the docs for
> more info.
> 
> This is definitely the most angry forum I've ever seen... the kicker
> is that the anger is almost always coming from the people associated
> with the django project... hmmm..
> 
> On May 6, 12:48 pm, jrs_66  wrote:
>> Hi,
>>
>> I have 2 models...
>>
>> class Category(models.Model):
>> name = models.CharField(max_length=255)
>> parent = models.ForeignKey('self', null=True)
>> has_children = models.BooleanField(default=False)
>> language = models.ForeignKey(Language, null=False, default=1)
>> active = models.BooleanField(default=False)
>> priority = models.IntegerField(null=False, default=5)
>> display_treatment = models.IntegerField(null=False, default=5)
>> last_update = models.DateTimeField(auto_now=True)
>>
>> def __unicode__(self):
>> return self.name
>>
>> class Meta:
>> verbose_name_plural = "Categories"
>> db_table = u'categories'
>>
>> class FlattenedCategory(models.Model):
>> category = models.ForeignKey(Category)
>> member_of_category = models.ForeignKey(Category,
>> related_name='memberof')
>> language = models.ForeignKey(Language)
>> hidden = models.IntegerField()
>> class Meta:
>> db_table = u'flattened_category'
>>
>> I have a Queryset...
>>
>> e = FlattenedCategory.objects.select_related('category').filter
>> (member_of_category=15)
>>
>> which works... This, however, doesn't
>>
>> e = e.category
>>
>> How do I access the related records from the Category model?
>>
>> Thanks.
>>
>> ps... Please refrain from commenting if the best you have is 'read the
>> docs'...docs'...
> > 

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Django and a basic SQL join

2009-05-06 Thread mamco

jrs_66:
you may find some comfort in the
http://ubuntuforums.org/forumdisplay.php?f=39
forum - not specific to django, but quite a friendly bunch and lots of
python folks willing to assist with the learning process.
tag your posts with 'django'



On May 6, 2:17 pm, jrs_66  wrote:
>
> This is definitely the most angry forum I've ever seen... the kicker
> is that the anger is almost always coming from the people associated
> with the django project... hmmm..
>

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Django and a basic SQL join

2009-05-06 Thread jrs_66

No...   'QuerySet' object has no
attribute 'category_set'

George,

I have read the docs... MANY times... from the docs...

'Django also creates API accessors for the "other" side of the
relationship -- the link from the related model to the model that
defines the relationship. For example, a Blog object b has access to a
list of all related Entry objects via the entry_set attribute:
b.entry_set.all().'

If you would look at my model, you will see that I'm not attempting to
join 'backwords'... forwards, my man... please review the docs for
more info.

This is definitely the most angry forum I've ever seen... the kicker
is that the anger is almost always coming from the people associated
with the django project... hmmm..

On May 6, 12:48 pm, jrs_66  wrote:
> Hi,
>
> I have 2 models...
>
> class Category(models.Model):
>     name = models.CharField(max_length=255)
>     parent = models.ForeignKey('self', null=True)
>     has_children = models.BooleanField(default=False)
>     language = models.ForeignKey(Language, null=False, default=1)
>     active = models.BooleanField(default=False)
>     priority = models.IntegerField(null=False, default=5)
>     display_treatment = models.IntegerField(null=False, default=5)
>     last_update = models.DateTimeField(auto_now=True)
>
>     def __unicode__(self):
>         return self.name
>
>     class Meta:
>         verbose_name_plural = "Categories"
>         db_table = u'categories'
>
> class FlattenedCategory(models.Model):
>     category = models.ForeignKey(Category)
>     member_of_category = models.ForeignKey(Category,
> related_name='memberof')
>     language = models.ForeignKey(Language)
>     hidden = models.IntegerField()
>     class Meta:
>         db_table = u'flattened_category'
>
> I have a Queryset...
>
> e = FlattenedCategory.objects.select_related('category').filter
> (member_of_category=15)
>
> which works... This, however, doesn't
>
> e = e.category
>
> How do I access the related records from the Category model?
>
> Thanks.
>
> ps... Please refrain from commenting if the best you have is 'read the
> docs'...the
> docs'...
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Django and a basic SQL join

2009-05-06 Thread George Song

On 5/6/2009 9:48 AM, jrs_66 wrote:
> I have 2 models...
> 
> class Category(models.Model):
> name = models.CharField(max_length=255)
> parent = models.ForeignKey('self', null=True)
> has_children = models.BooleanField(default=False)
> language = models.ForeignKey(Language, null=False, default=1)
> active = models.BooleanField(default=False)
> priority = models.IntegerField(null=False, default=5)
> display_treatment = models.IntegerField(null=False, default=5)
> last_update = models.DateTimeField(auto_now=True)
> 
> def __unicode__(self):
> return self.name
> 
> class Meta:
> verbose_name_plural = "Categories"
> db_table = u'categories'
> 
> class FlattenedCategory(models.Model):
> category = models.ForeignKey(Category)
> member_of_category = models.ForeignKey(Category,
> related_name='memberof')
> language = models.ForeignKey(Language)
> hidden = models.IntegerField()
> class Meta:
> db_table = u'flattened_category'
> 
> 
> I have a Queryset...
> 
> e = FlattenedCategory.objects.select_related('category').filter
> (member_of_category=15)
> 
> which works... This, however, doesn't
> 
> e = e.category
> 
> How do I access the related records from the Category model?
> 
> Thanks.
> 
> ps... Please refrain from commenting if the best you have is 'read the
> docs'...

e.category_set is what you want.

And, yes, please read the docs.

-- 
George

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Django and a basic SQL join

2009-05-06 Thread jrs_66

Hi,

I have 2 models...

class Category(models.Model):
name = models.CharField(max_length=255)
parent = models.ForeignKey('self', null=True)
has_children = models.BooleanField(default=False)
language = models.ForeignKey(Language, null=False, default=1)
active = models.BooleanField(default=False)
priority = models.IntegerField(null=False, default=5)
display_treatment = models.IntegerField(null=False, default=5)
last_update = models.DateTimeField(auto_now=True)

def __unicode__(self):
return self.name

class Meta:
verbose_name_plural = "Categories"
db_table = u'categories'

class FlattenedCategory(models.Model):
category = models.ForeignKey(Category)
member_of_category = models.ForeignKey(Category,
related_name='memberof')
language = models.ForeignKey(Language)
hidden = models.IntegerField()
class Meta:
db_table = u'flattened_category'


I have a Queryset...

e = FlattenedCategory.objects.select_related('category').filter
(member_of_category=15)

which works... This, however, doesn't

e = e.category

How do I access the related records from the Category model?

Thanks.

ps... Please refrain from commenting if the best you have is 'read the
docs'...
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---