uwain12345
uwain12345

Reputation: 386

Adding all users of a group as choices in a model field

I'm trying to restrict a field in one of my models to a choice of all users that are members of a particular group. Another field has all registered users a choices. This works fine in the shell:

>>> from django.contrib.auth.models import User, Group
>>> all_users = User.objects.all()
>>> all_users
<QuerySet [<User: somedude>, <User: test>, <User: testma>]>
>>> tm_users = User.objects.filter(groups__name='Testmanager')
>>> tm_users
<QuerySet [<User: somedude>, <User: testma>]>

But if I put the same code in the model:

class TestPlan(models.Model):

    all_users = User.objects.all()
    all_user_choices = ((x.username, x.get_full_name()) for x in all_users)
    tm_users = User.objects.filter(groups__name='Testmanager')
    tm_user_choices = ((x.username, x.get_full_name()) for x in tm_users)
    tp_title = models.CharField(max_length=200, verbose_name='Title')
    tp_manager = models.CharField(max_length=100, blank=True,
                                 verbose_name='Test Manager',
                                  choices=tm_user_choices)
    tp_tester = models.CharField(max_length=100, blank=True,
                                 verbose_name='Tester',
                                 choices=all_user_choices)


    def __str__(self):
        return f'{self.tp_title}'

I get the following less than helpful (to me) stack trace:

O:\somedude\Documents\Python\Django\testcases>manage.py makemigrations
Traceback (most recent call last):
  File "O:\somedude\Documents\Python\Django\testcases\manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 381, i
n execute_from_command_line
    utility.execute()
  File "C:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 357, i
n execute
    django.setup()
  File "C:\Program Files\Python36\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Program Files\Python36\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "C:\Program Files\Python36\lib\site-packages\django\apps\config.py", line 198, in import_mode
ls
    self.models_module = import_module(models_module_name)
  File "C:\Program Files\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "O:\somedude\Documents\Python\Django\testcases\testman\models.py", line 68, in <module>
    class TestPlan(models.Model):
  File "O:\somedude\Documents\Python\Django\testcases\testman\models.py", line 70, in TestPlan
    tm_users = User.objects.filter(groups__name='Testmanager')
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\manager.py", line 82, in manage
r_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\query.py", line 844, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\query.py", line 862, in _filter
_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\query.py", line 1263, in ad
d_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\query.py", line 1287, in _a
dd_q
    split_subq=split_subq,
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\query.py", line 1164, in bu
ild_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg)
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\query.py", line 1028, in so
lve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\query.py", line 1365, in na
mes_to_path
    if field.is_relation and not field.related_model:
  File "C:\Program Files\Python36\lib\site-packages\django\utils\functional.py", line 37, in __get__

    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\fields\related.py", line 94, in
 related_model
    apps.check_models_ready()
  File "C:\Program Files\Python36\lib\site-packages\django\apps\registry.py", line 137, in check_mod
els_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

O:\somedude\Documents\Python\Django\testcases>

It works when I do User.objects.all() but it falls over when I try to filter the users by group. I really have no idea what I'm doing wrong here. Is this a bug or a feature?

Upvotes: 2

Views: 1445

Answers (3)

Atley Varghese
Atley Varghese

Reputation: 576

You may have to override the save method and clean method:

from django.core.exceptions import ValidationError

def clean(self):
    if self.tp_manager and not self.tp_manager.groups.filter(name='Testmanager').exists():
        raise ValidationError({'tp_manager': 'ValidationError Msg'})

def save(self, *args, **kwargs):
    self.full_clean()
    return super().save(*args, **kwargs)

Django Docs: https://docs.djangoproject.com/en/2.0/ref/models/instances/#django.db.models.Model.clean

Otherwise, you can use a ModelForm and write a clean function, to implement your validation check.

If you are to use a form then you can easily handle the dynamic choices.

class TestPlanForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        tm_users = User.objects.filter(groups__name='Testmanager')
        tm_user_choices = ((x.username, x.get_full_name()) for x in tm_users)
        self.fields['tp_manager'].choices = tm_user_choices

    class Meta:
        model = TestPlan
        fields = '__all__'

Upvotes: 1

uwain12345
uwain12345

Reputation: 386

While I haven't found an answer to the original question, I have solved the underlying issue. The way to do this is to define the model fields as foreign keys:

class TestPlan(models.Model):
    tp_title = models.CharField(max_length=200, verbose_name='Title')
    tp_manager = models.ForeignKey(User, on_delete=models.SET_NULL,
                    blank=True, null=True, verbose_name='Test Manager',
                    limit_choices_to={'groups__name': 'Testmanager'},
                    related_name='testmanager_set')
    tp_tester = models.ForeignKey(User, on_delete=models.SET_NULL,
                                  blank=True, null=True, verbose_name='Tester')

The limit_choices_to parameter does the same thing as User.objects.filter(groups__name='Testmanager') in the original post. The related_name parameter is needed because I now have two foreign key fields on the same model.

Upvotes: 0

josemlp
josemlp

Reputation: 506

The "choices" field property can't be dynamic, necessarily must be a static set, for example:

MONTH_CHOICES = (
    ("JANUARY", "January"),
    ("FEBRUARY", "February"),
    ("MARCH", "March"),
    # ....
    ("DECEMBER", "December"),
)

If you need this restriction, you can validate this case in the "django form" or "DRF serializer" logic.

Upvotes: 1

Related Questions