1๐
โ
# models.py
class News(models.Model):
group = models.ForeignKey(Group)
users = models.ManyToManyField(User)
# forms.py
class NewsAdminForm(forms.ModelForm):
class Meta:
model = News
def __init__(self, *args, **kwargs):
super(News, self).__init__(*args, **kwargs)
if self.instance.group is not None:
# here self.instance.group is need you group (programmers, managers etc.)
users_queryset = User.objects.filter(groups=self.instance.group)
self.filelds['users'].choices = [(u.id, u.username) for u in users_queryset)
# admin.py
class NewsAdmin(admin.ModelAdmin):
form = NewsAdminForm
๐คfreylis
Source:stackexchange.com