I have constructed the below model. I think this can be easily be asked but I am really stuck to that!
part of the Model
class Task(models.Model):
Taskdetails = models.CharField(max_length=500, null=True)
employee = models.ForeignKey("auth.User", null = True)
def __str__(self):
return str(self.id)
Form
class TaskForm (ModelForm):
class Meta:
model = Task
fields = ('Taskdetails','employee',)
part of the views
def task_new(request):
if request.method == "POST":
task_form = TaskForm(request.POST)
if task_form.is_valid():
task_form.employee = User.objects.filter(groups__name='supervisor')
So in my template while I create a model instance, in the dropdown menu, i am trying to display only the users that belong to a certain group (in this example in the group supervisor). On the contrary, in the template the dropdown menu shows all the users, not taking into consideration the filter that I put in views.
To sum up, I am trying to figure out in Django 1.9 edition to filter the users by groups.
Also important to mention that i am using the default User model so the attribute group is not declared in the model. When I write the below line to the Python console, it shows only the users that belong to the group "supervisor".
User.objects.filter(groups__name='supervisor')