0

我是 Django 的新手,我有一个包含三个模型的应用程序。我需要用户创建一个提要,但只在其组中。例如,我有 2 个品牌和 2 个用户,一个用户分配给一个品牌,另一个用户分配给另一个品牌。当我想在品牌选择中与用户创建提要时,会出现所有品牌,我希望只显示与用户关联的品牌。我该如何解决?

我有这些课程

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
4

1 回答 1

1

我从问题中了解到的。可能是这样的:

   feed = Feed.objects.filter(barnd__in=user.brands.all())
于 2012-11-28T03:25:12.443 回答