1

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')  
ekad
  • 14,436
  • 26
  • 44
  • 46
Sadi Noel
  • 17
  • 6
  • This might help : http://stackoverflow.com/questions/1810891/django-how-to-filter-users-that-belong-to-a-specific-group – Abijith Mg Mar 16 '17 at 18:52

1 Answers1

1

You want to limit the employees to certain users (supervisors) when displaying the form, correct?

Then I don't understand why you set the employee field of the form to a certain value after you posted the form.

What you probably want is something like this:

class TaskForm(forms.ModelForm):
    class Meta:
        model = Task
        fields = ('Taskdetails','employee',)

    def __init__(self, *args,**kwargs):
        super (TaskForm,self ).__init__(*args,**kwargs)
        self.fields['employee'].queryset = User.objects.filter(groups__name='supervisor')
arie
  • 18,737
  • 5
  • 70
  • 76
  • It almost works! I forgot to mention another important part: After I save this instance, another user who belongs to a different group needs to edit the **Same Form** (different view-template) but in this second form (which is the same form as before), is needed to display users from a different group (eg "technicians"). If i declare in init, groups_name = supervisor, then in each form only the users that belong to the group supervisor will be displayed! Is there any way to do this?Thank you for your help! – Sadi Noel Mar 18 '17 at 07:44
  • Check this answer: http://stackoverflow.com/a/8841565/630877 Basically you have to additionally check the `request.user` in your form. Based on the user you then can use a different filter for the employee-queryset ( `groups__name=...`.) – arie Mar 18 '17 at 09:14