M
M
Meekey2018-03-05 20:04:06
Django
Meekey, 2018-03-05 20:04:06

How to work with ManyToManyField Django?

How to get a choice from one to several Contractors (owner) from the "Technical support" group, in the selection field in the application in the admin panel.
Now I can set it up only to receive one, and no more, performers.
Used ForeignKey:

class Application(models.Model):
    ...
    owner = models.ForeignKey(User, null = True, blank = True, limit_choices_to={ 'groups__name': 'Техническая поддержка'})

After reading, I realized that you can get them through ManyToManyField, and did the following:
class Owner(models.Model):
    executor = models.ForeignKey(User, null = True, blank = True, limit_choices_to={ 'groups__name': 'Техническая поддержка'})


class Application(models.Model):
    ...
    owner = models.ManyToManyField(Owner, verbose_name = 'Исполнитель')

But, the executor is taken not from the "Technical support" group, but from the "executor", which still needs to be created. And even if you create, the choice will be as follows:
5a9d77f5e8674049679307.png
In general, I just can’t figure it out. Many thanks to all those who try to help with this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Malyarov, 2018-03-05
@Konstantin18ko

I don’t know what kind of fields you have there in the model, but here is an example:

class Owner(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return '{0}'.format(self.short_name)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question