我有一个在 html 发布页面上显示客户端资产的应用程序。每个被授权使用系统的客户都被分配了一个配置文件:
class UserProfile(models.Model):  
    user = models.ForeignKey(User, unique=True)
    fullname = models.CharField(max_length=64, unique=False)
    company = models.CharField(max_length=50, choices=CLIENT_CHOICES)
    position = models.CharField(max_length=64, unique=False, blank=True, null=True)
    ...
    User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
    def __unicode__(self):
        return u'%s' % self.fullname
    class Meta:
        ordering = ['fullname']
    class Admin: 
        pass  
并且有一个帖子页面的模型:
class PostPage(models.Model):
    client = models.CharField(max_length=50, choices=CLIENT_CHOICES)
    job_number = models.CharField(max_length=30, unique=True, blank=False, null=False)
    job_name = models.CharField(max_length=64, unique=False, blank=False, null=False)
    page_type = models.CharField(max_length=50, default='POST')
    create_date = models.DateField(("Date"), default=datetime.date.today)
    contact = models.ForeignKey(UserProfile)
    contact2 = models.ForeignKey(UserProfile, related_name='+', blank=True, null=True)
    contact3 = models.ForeignKey(UserProfile, related_name='+', blank=True, null=True)
    contact4 = models.ForeignKey(UserProfile, related_name='+', blank=True, null=True)
    def __unicode__ (self):
            return u'%s %s %s' % (self.client, self.job_number, self.job_name)
    class Admin: 
            pass
最后,一个非常简单的视图函数来显示页面:
def display_postings(request, job_number):
        records = PostPage.objects.filter(job_number=job_number)
        tpl = 'post_page.html'
        return render_to_response(tpl, { 'records': records })
问题是,如果您为“ACME”公司工作并访问系统,则视图中没有任何逻辑可以阻止您查看除您自己之外的“BETAMAX”公司的记录。如何修改我的视图,以便如果说 user.profile.company = "ACME" ,但请求返回 PostPage.client = "BETAMAX" 的记录,则拒绝访问该记录?另外,我可以有一个公司组,比如 user.profile.company = "MY_COMPANY" 可以访问所有记录吗?