检查外键对象的id

时间:2013-11-05 14:14:28

标签: django django-models django-middleware

我正在尝试编写一个函数来检查用户是否具有正确的成员类别。

我的模特:

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


class Membercategory(models.Model):
    membercatname = models.CharField(max_length=200)  


class Profile(models.Model):
    user = models.OneToOneField(User)
    mobilephone = models.CharField(max_length=12, blank=True)  
    membercategory = models.ForeignKey(Membercategory, blank=True, null=True)

我的用户检查中间件:

from django.http import HttpResponseRedirect
from django.conf import settings

class UserCheckMiddleware:

    def process_request(self, request):

        invalid_user_path = settings.INVALID_USER_URL

        if request.user.is_authenticated() and not request.user.is_staff:

            if not request.user.profile.membercategory(id=426):
                return HttpResponseRedirect(invalid_user_path)
        else:
            return        

我得到的错误是:Membercategory object is not callable

我应该如何检查?

1 个答案:

答案 0 :(得分:0)

好吧,正如错误所说,Django模型实例不可调用。您只想与ID进行简单比较:

if request.user.profile.membercategory_id != 426: