Django QuerySets, how to express EXISTS ... IN ...

2015-02-08 Thread Dean De Leo
Hello,
I am trying to find how to filter the entries of my blog by a set of
tags, using the Django models.
In particular there are two relations:
Blog (id, title, content)
Tag ( name, blog_id )
with Tag.blog_id foreign key of Blog.id
I want to select all blog entries that contain a certain set of tags.
For instance, I would express the query in SQL as:
SELECT * FROM blog_blog b WHERE EXISTS ( SELECT 1 FROM blog_tag t WHERE
t.blog_id = b.id AND lower(t.name) IN ('tag1', 'tag2', 'tag3')  );

How to represent the same query with the Django QuerySets ?

Thanks,
Dean De Leo


-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/54D78EFA.4030302%40gmx.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django QuerySets, how to express EXISTS ... IN ...

2015-02-08 Thread Neto
mytag = Tag.objects.get(id=1)
mytag.blog_set.all()

Em domingo, 8 de fevereiro de 2015 14:30:25 UTC-2, Dean De Leo escreveu:
>
> Hello, 
> I am trying to find how to filter the entries of my blog by a set of 
> tags, using the Django models. 
> In particular there are two relations: 
> Blog (id, title, content) 
> Tag ( name, blog_id ) 
> with Tag.blog_id foreign key of Blog.id 
> I want to select all blog entries that contain a certain set of tags. 
> For instance, I would express the query in SQL as: 
> SELECT * FROM blog_blog b WHERE EXISTS ( SELECT 1 FROM blog_tag t WHERE 
> t.blog_id = b.id AND lower(t.name) IN ('tag1', 'tag2', 'tag3')  ); 
>
> How to represent the same query with the Django QuerySets ? 
>
> Thanks, 
> Dean De Leo 
>
>
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/1f6e32ea-c2ec-4552-aabf-19e6840d753c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django QuerySets, how to express EXISTS ... IN ...

2015-02-08 Thread Dean De Leo
Hi, thanks for the reply.
I still get the error:
Exception Type: AttributeError
Exception Value:

'Tag' object has no attribute 'blog_set'


if rawtags:
taglist = rawtags.split('/');
t = Tag.objects.get(name = "Alpha");
blogentries = t.blog_set.all()
else:
blogentries = Blog.objects.all();



My Models:

# My blog
class Blog(models.Model):
title = models.CharField(max_length=255)
content = models.TextField(max_length=1);
dateAdd = models.DateTimeField(auto_now_add=True, null=False);
dateEdit = models.DateTimeField(auto_now=True);

class Meta:
ordering = ['-id']


class Tag(models.Model):
name = models.CharField(max_length=255, null=False)
blog = models.ForeignKey('Blog', null=False)

class Meta:
unique_together = [("name", "blog",)]
ordering = ["name"]


Am I mispelling something?

Thanks,
Dean De Leo



On 08/02/15 16:55, Neto wrote:
> mytag = Tag.objects.get(id=1)
> mytag.blog_set.all()
>
> Em domingo, 8 de fevereiro de 2015 14:30:25 UTC-2, Dean De Leo escreveu:
>
> Hello,
> I am trying to find how to filter the entries of my blog by a set of
> tags, using the Django models.
> In particular there are two relations:
> Blog (id, title, content)
> Tag ( name, blog_id )
> with Tag.blog_id foreign key of Blog.id
> I want to select all blog entries that contain a certain set of tags.
> For instance, I would express the query in SQL as:
> SELECT * FROM blog_blog b WHERE EXISTS ( SELECT 1 FROM blog_tag t
> WHERE
> t.blog_id = b.id  AND lower(t.name )
> IN ('tag1', 'tag2', 'tag3')  );
>
> How to represent the same query with the Django QuerySets ?
>
> Thanks,
> Dean De Leo
>
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/54D7A007.7070405%40gmx.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django QuerySets, how to express EXISTS ... IN ...

2015-02-08 Thread Neto
Your model is incorrect. Each blog has only one tag? So you have only one 
result per tag. 
Tag.objects.get(name = "Alpha").blog

Em domingo, 8 de fevereiro de 2015 15:43:33 UTC-2, Dean De Leo escreveu:
>
>  Hi, thanks for the reply.
> I still get the error: 
>   Exception Type: AttributeError  Exception Value: 
>
> 'Tag' object has no attribute 'blog_set'
>
>   
> if rawtags:
> taglist = rawtags.split('/');t = Tag.objects.get(name = "Alpha");
> blogentries = t.blog_set.all()else:
> blogentries = Blog.objects.all();
>
>
>
> My Models:
>
> # My blogclass Blog(models.Model):
> title = models.CharField(max_length=255)
> content = models.TextField(max_length=1);
> dateAdd = models.DateTimeField(auto_now_add=True, null=False);
> dateEdit = models.DateTimeField(auto_now=True);
>
> class Meta:
> ordering = ['-id']
>
> class Tag(models.Model):
> name = models.CharField(max_length=255, null=False)
> blog = models.ForeignKey('Blog', null=False)
>
> class Meta:
> unique_together = [("name", "blog",)]
> ordering = ["name"]
>
>
> Am I mispelling something?
>
> Thanks,
> Dean De Leo
>
>
>
> On 08/02/15 16:55, Neto wrote:
>  
>  mytag = Tag.objects.get(id=1)
>  mytag.blog_set.all()
>
> Em domingo, 8 de fevereiro de 2015 14:30:25 UTC-2, Dean De Leo escreveu: 
>>
>> Hello, 
>> I am trying to find how to filter the entries of my blog by a set of 
>> tags, using the Django models. 
>> In particular there are two relations: 
>> Blog (id, title, content) 
>> Tag ( name, blog_id ) 
>> with Tag.blog_id foreign key of Blog.id 
>> I want to select all blog entries that contain a certain set of tags. 
>> For instance, I would express the query in SQL as: 
>> SELECT * FROM blog_blog b WHERE EXISTS ( SELECT 1 FROM blog_tag t WHERE 
>> t.blog_id = b.id AND lower(t.name) IN ('tag1', 'tag2', 'tag3')  ); 
>>
>> How to represent the same query with the Django QuerySets ? 
>>
>> Thanks, 
>> Dean De Leo 
>>
>>
>>   
>  

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/aadb6aac-ade4-4fe8-9d0b-d0a9ec74d159%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.