Re: possible to filter ManyToMany results?

2006-04-19 Thread nkeric

Luke Plant wrote:
> As I understand it, Django should be able to do this query no problem.
> I think there may be a mistake in what you posted though - IIRC, I
> would have expected the method name to be 'get_articles_list' not
> 'get_article_list', since you defined it as 'articles =
> meta.ManyToManyField(Article)'.  If you post the entire code for your
> models we may be able to find the problem.  Magic-removal branch
> doesn't really change what is possible here, it just changes the
> syntax.
>

hi Luke, Ivan!

I've migrated my new app to the m-r branch, it's AWESOME!

it's really simple to get things done:

[code]
gamesku = GameSku.objects.get(pk=gamesku_id)
article_list =
gamesku.articles.filter(article_type__name__exact=article_type).order_by('-submit_date')
[/code]

that's what I want :D

and, here is my **old** models definition, I don't know if it's
possible to do so with django trunk:

class ArticleType(meta.Model):
name = meta.CharField(maxlength=128)
display_name = meta.CharField(maxlength=128)
display_order = meta.IntegerField(blank=False)
description = meta.CharField(maxlength=2048)

class Article(meta.Model):
user = meta.ForeignKey(auth.User)
article_type = meta.ForeignKey(ArticleType)
title = meta.CharField(_('title'), maxlength=255, blank=False)
description = meta.TextField(_('description'), maxlength=500)
content = meta.TextField(_('content'), maxlength=3000)
submit_date = meta.DateTimeField(_('date/time submitted'),
auto_now_add=True)

class GameSku(meta.Model):
game = meta.ForeignKey(Game)
platform = meta.ForeignKey(GamingPlatform)
developer = meta.ForeignKey(Company, verbose_name='Developer')
publisher = meta.ForeignKey(Company, verbose_name='Publiser')
name = meta.CharField(maxlength=128)
articles = meta.ManyToManyField(Article)

the sql I wrote to get what I want is:

sql = "select distinct articles.id as id from articles, article_type,
game_sku_articles, game_sku where article_type.name =
'%(article_type)s' and articles.article_type_id = article_type.id and
game_sku.id = '%(gamesku_id)d' and game_sku_articles.gamesku_id =
game_sku.id and game_sku_articles.article_id = articles.id" % locals()

I was wondering how to "get a spefic type of articles of a gamesku
while gamesku & articles' relationship is many-to-many" in django
trunk..

BiG thanks to all you guys!

I used to be someone who didn't like django's ORM stuff, however, in
the new m-r branch, with brand-new ORM support and transaction support,
I'm LOVING it! 

Django rocks! :)

- nkeric


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: possible to filter ManyToMany results?

2006-04-14 Thread Ivan Sagalaev

Luke Plant wrote:

>As I understand it, Django should be able to do this query no problem.  
>  
>
I also thought so :-). I created a test project with this model and 
tried to get this query to work. "get_article_list" is indeed there but 
it doesn't like 'article_type__id__exact' as an argument.

I think this is a limitation of current trunk (may be a bug) that you 
can't have additional joins in selects from M-M relations.

But still, may be I just didn't search good enough...

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: possible to filter ManyToMany results?

2006-04-14 Thread Ivan Sagalaev

nkeric wrote:

>Really? That's cool!
>  
>
Yes. One of the great new features of m-r branch is that relation 
queries work similary for M-M and 1-M relations and in both directions:

game.articles.filter(article_type__pk=1) # all articles for one game 
of one type
article.game_set.all() # all games from one article

and even

article.game_set.filter(article__article_type__pk=1)

... which gives all games mentioned in the article that was mentioned in 
other articles of certain type (though it's not very practicle :-) )

>Our site (http://www.ifaxian.com - a Chinese Digg-like site) built upon
>the svn trunk version, we're planning to migrate to the m-r branch
>soon, I guess it's time for me to take a deeper look into the m-r
>branch :)
>  
>
My advice is to making it sooner than later. I just killed the whole day 
yesterday refactoring my project (medium sized mp3 exchange service with 
wishlists, catalog, search and a blog) to magic removal. The most time I 
spent converting old SQL queries to new ORM calls. So the more old stuff 
you have the more you will have to convert.

If you fancy an example... To track downloads I have a DownloadLog table 
that basically just references Users and Albums with foreign keys. There 
was one query that selects "albums downloaded by users that have 
downloaded this album". In trunk version there was an SQL of two nested 
selects that looked pretty scary. Now it's just one line:


Album.objects.filter(downloadlog__user__downloadlog__album__pk=self.id).exclude(pk=self.id).distinct()

... which almost can be read in English ("albums in logs with users in 
logs with this album").

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: possible to filter ManyToMany results?

2006-04-14 Thread Luke Plant

On Wednesday 12 April 2006 07:03, nkeric wrote:
> hi all,
>
> I've done some search, however, I guess I should ask for your helps
> here:
>
> I have the following models: (pseudo code)
>
> class ArticleType:
> name = ...
>
> class Article:
> title = ...
> article_type = meta.ForeignKey(ArticleType)
>
> class Game:
> name = ...
> articles = meta.ManyToManyField(Article)
>
> how can I retrive the articles of a certain article type of a given
> game?
>
> >>> g = games.get_object(pk=1)
> >>> g.get_article_list(article_type_id__exact=1)
>
> doesn't work, though I knew how to do this in raw sql...

As I understand it, Django should be able to do this query no problem.  
I think there may be a mistake in what you posted though - IIRC, I 
would have expected the method name to be 'get_articles_list' not 
'get_article_list', since you defined it as 'articles = 
meta.ManyToManyField(Article)'.  If you post the entire code for your 
models we may be able to find the problem.  Magic-removal branch 
doesn't really change what is possible here, it just changes the 
syntax.

Luke

-- 
"Humiliation: The harder you try, the dumber you look." (despair.com)

Luke Plant || L.Plant.98 (at) cantab.net || http://lukeplant.me.uk/

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: possible to filter ManyToMany results?

2006-04-14 Thread Ivan Sagalaev

nkeric wrote:

>Ivan, thanks a lot for your reply :)
>well, I guess I have to take the "dirty" sql approach :p
>  
>
You might want to look into magic-removal branch where things like this 
are made possible with ORM.

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: possible to filter ManyToMany results?

2006-04-13 Thread nkeric

Ivan Sagalaev wrote:
> nkeric wrote:
>
> >hello, could anybody pls help? I don't want to write dirty sql if I
> >could do it via django ORM...
> >
> >
> Well... The only thing I could work out is this:
>
> articles = [a for a in g.get_article_list() if a.article_type_id == 1]

Ivan, thanks a lot for your reply :)
well, I guess I have to take the "dirty" sql approach :p

Regards,

- Eric


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: possible to filter ManyToMany results?

2006-04-12 Thread Ivan Sagalaev

nkeric wrote:

>hello, could anybody pls help? I don't want to write dirty sql if I
>could do it via django ORM...
>  
>
Well... The only thing I could work out is this:

articles = [a for a in g.get_article_list() if a.article_type_id == 1]

Drawbacks are obvious: it's always selects all articles for a game 
(which may be slow) and it can filter type only be id. But it can be a 
viable solution.

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: possible to filter ManyToMany results?

2006-04-12 Thread nkeric

hello, could anybody pls help? I don't want to write dirty sql if I
could do it via django ORM...

BiG thanks! :)


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: possible to filter ManyToMany results?

2006-04-12 Thread nkeric

Ivan Sagalaev wrote:
> g.get_article_list(article_type_id__exact=1)
> It's g.get_article_list(article_type__id__exact=1). Note 2 underscores
> before "id" which means field "id" of a parent table.

sorry for mistyped that, I tried this:

>>> g.get_article_list(article_type__id__exact=1)
Traceback (most recent call last):
  File "", line 1, in ?
  File "/usr/lib/python2.4/site-packages/django/utils/functional.py",
line 3, in _curried
TypeError: method_get_many_to_many() got an unexpected keyword argument
'article_type__id__exact'

doesn't work either... :(


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: possible to filter ManyToMany results?

2006-04-12 Thread Ivan Sagalaev

nkeric wrote:

>class ArticleType:
>name = ...
>
>class Article:
>title = ...
>article_type = meta.ForeignKey(ArticleType)
>
>class Game:
>name = ...
>articles = meta.ManyToManyField(Article)
>
>how can I retrive the articles of a certain article type of a given
>game?
>
>  
>
g = games.get_object(pk=1)
g.get_article_list(article_type_id__exact=1)


It's g.get_article_list(article_type__id__exact=1). Note 2 underscores 
before "id" which means field "id" of a parent table.

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---