0

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

我的模型:

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

我应该怎么做检查?

4

1 回答 1

0

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

if request.user.profile.membercategory_id != 426:
于 2013-11-05T14:20:54.737 回答