Meekey
Meekey

Reputation: 59

Error getting users from group django

Now I'm working on a system for filing applications for repairing equipment in one organization. Each application can be owned by a certain person from the group (Which is set in the admin panel) of technical support. The model of the application is partially written, but I can not figure out the purpose of the application for this application.

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

What am I doing wrong? Maybe you have some advice?

If I do so,

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

Then there is a migration error

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.

Upvotes: 0

Views: 188

Answers (1)

H&#229;ken Lid
H&#229;ken Lid

Reputation: 23064

It seems that the Owner class' only purpose is to represent a relation between Application and User. You can instead have a direct m2m, and use limit_choices_to to filter only users with the correct permissions (and name, but that seems like a bad idea to hard code)

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,
    )

You can also use a function, if you want to do some complicated filtering during runtime. Here's an example:

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)

Upvotes: 1

Related Questions