samedi 29 novembre 2014

python - django recherche de relation clé étrangère - Stack Overflow


Given the following models:


class Post(models.Model):
title = models.CharField(max_length=200)
html = models.TextField()

class PostTag(models.Model):
post = models.ForeignKey('Post')
tag = models.CharField(max_length=200)

I want to accomplish looking up a Post based on a given PostTag. So if I had two posts, A and B, tagged as "foo", I want to be able to look up all posts with that tag and get the posts A and B back.


I image the query would look something like the following:


posts = Post.objects.filter(tag=tag)

Any tips on where to start to accomplish this?




Close. You need to specify which field you're spanning across.


Post.objects.filter(posttag__tag=tag)


Given the following models:


class Post(models.Model):
title = models.CharField(max_length=200)
html = models.TextField()

class PostTag(models.Model):
post = models.ForeignKey('Post')
tag = models.CharField(max_length=200)

I want to accomplish looking up a Post based on a given PostTag. So if I had two posts, A and B, tagged as "foo", I want to be able to look up all posts with that tag and get the posts A and B back.


I image the query would look something like the following:


posts = Post.objects.filter(tag=tag)

Any tips on where to start to accomplish this?



Close. You need to specify which field you're spanning across.


Post.objects.filter(posttag__tag=tag)

0 commentaires:

Enregistrer un commentaire