从组django获取用户时出错

时间:2018-02-24 18:08:48

标签: python django

现在,我正在研究一个系统,用于在一个组织中提交修复设备的申请。每个应用程序都可以由技术支持的组(在管理面板中设置)中的某个人拥有。 应用程序的模型是部分编写的,但我无法弄清楚该应用程序的应用程序的目的。

models.py

class Owner(models.Model):
OWNER_CHOICES = []
    for user in User.objects.filter(name = 'Техническая поддержка'):
        if user.has_perm('helpdesk.can_close'):
            OWNER_CHOICES.append(user)

    name = models.CharField(max_length=50, choices=OWNER_CHOICES)

    def __str__(self):
        return self.name

 class Application(models.Model):

     STATUS_CHOICES = (
        ('in_the_work', 'В работе'),
        ('new', 'Новая'),
        ('completed', 'Завершена')
    )

    authors = models.ForeignKey('auth.User')
    title = models.CharField(max_length=50)
    text = models.TextField()
    room = models.CharField(max_length = 4)
    published_date = models.DateField(blank=True, null=True, default = datetime.datetime.now)
    status = models.CharField(max_length=15, choices=STATUS_CHOICES, default='new')
    owner = models.ManyToManyField(Owner)

class Meta:
    permissions = (
        ("can_add_change", "Добавлять и изменять"),
        ("can_close", "Закрывать"),
        ("can_assign", "Назначать"),
    )

    def publish(self):
        self.publish_date = datetime.datetime.now()
        self.save()

    def __str__(self):
        return self.title

我做错了什么?也许你有一些建议?

如果我这样做,

class Owner(models.Model):
OWNER_CHOICES = []
for user in Group.objects.filter(name = 'Техническая поддержка'):
    #if user.has_perm('helpdesk.can_close'):
        OWNER_CHOICES.append(user)

name = models.CharField(max_length=50, choices=OWNER_CHOICES)

def __str__(self):
    return self.name

然后出现迁移错误

SystemCheckError: System check identified some issues:

ERRORS:
<class 'helpdesk.admin.ApplicationAdmin'>: (admin.E002) The value of 
'raw_id_fields[0]' refers to 'user', which is not an attribute of 
'helpdesk.Application'.
<class 'helpdesk.admin.ApplicationAdmin'>: (admin.E108) The value of 
'list_display[0]' refers to 'user', which is not a callable, an attribute of 
'ApplicationAdmin', or an attribute or method on 'helpdesk.Application'.
<class 'helpdesk.admin.ApplicationAdmin'>: (admin.E116) The value of 
'list_filter[2]' refers to 'user', which does not refer to a Field.
helpdesk.Owner.name: (fields.E005) 'choices' must be an iterable containing 
(actual value, human readable name) tuples.

1 个答案:

答案 0 :(得分:0)

似乎Owner类的唯一目的是表示Application和User之间的关系。您可以改为使用直接m2m,并使用limit_choices_to仅过滤具有正确权限的用户(和名称,但这对硬编码来说似乎不太好)

limit_app_owner = (
    models.Q(user_permissions__codename='helpdesk.can_close') & 
    models.Q(name='Техническая поддержка')
)

class Application(models.Model):
    ...
    owner = models.ManyToManyField(
        User, 
        limit_choices_to=limit_app_owner,
    )

如果要在运行时进行一些复杂的过滤,也可以使用函数。这是一个例子:

def limit_app_owner():
    ids = []
    for user in User.objects.all():
        if user.has_perms('helpdesk.can_close'):
            ids.append(user.id)
    return models.Q(id__in=ids)