由记录的用户过滤字段

时间:2012-11-27 22:59:42

标签: python django

我是Django的新手,我有一个有三种型号的应用程序。我需要用户创建一个Feed,但只需在其组中。例如,我有2个品牌,2个用户,一个用户被指定为一个品牌而另一个用户被指定为另一个品牌。当我想要在品牌选择中创建一个用户的Feed时,所有品牌和我希望它只显示与用户相关联的品牌。我该如何解决这个问题?

我有这些课程

from django.db import models
from django.contrib.auth.models import User


class Brand(models.Model):
"""
Brand Model
"""
    name = models.CharField(max_length=60)

    def __unicode__(self):
        return self.name

class UserFeed(models.Model):
"""
User feed
"""
    user = models.OneToOneField(User)
    firstName = models.CharField(max_length=100)
    lastName = models.CharField(max_length=100)
    email = models.EmailField(max_length=100, blank=True)
    brands = models.ManyToManyField(Brand)

    def __unicode__(self):
        return self.firstName

class Feed(models.Model):
"""
Principal Model for feeds and news
"""
    title = models.CharField(max_length=200)
    url = models.URLField(max_length=200)
    pubdate = models.DateTimeField('Date published')
    brand = models.ForeignKey(Brand)
    image = models.ImageField(upload_to="images", max_length=100, blank=True)
    description = models.TextField()
    content = models.TextField()
    updated = models.DateTimeField('Date updated', auto_now=True)
    active = models.BooleanField()

    def __unicode__(self):
        return self.title

1 个答案:

答案 0 :(得分:1)

我从问题中理解的是什么。 Maby是这样的:

   feed = Feed.objects.filter(barnd__in=user.brands.all())