0

I am trying to implement a drop-down list in Django which contains the email addresses of those users whose domain matches to that of the currently logged in user.

I am using the following code but it still shows a list and not a drop down list. I tried changing the widget part from ChoiceField to MultipleChoiceField and now to CheckboxSelectMultiple just to see if checkbox is displayed or not but none of it seems to work.

Here is my forms.py

class AssignTask(forms.Form):
    title = forms.CharField(max_length=200)
    description = forms.CharField(widget=forms.Textarea)
    assign_to = forms.MultipleChoiceField(choices=[], widget=forms.CheckboxSelectMultiple, required=False)

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        user_email = self.user.email.split('@')[1]
        super(AssignTask, self).__init__(*args, **kwargs)
        self.fields['assign_to'] = forms.MultipleChoiceField(choices=[(i.email, i.email) for i in User.objects.filter(is_active=True, email__icontains=user_email)])

Here is my views.py

@login_required
def assigntask(request):
    assign_form = AssignTask(user=request.user)
    return render(request, 'todoapp/assign_task.html', context={'assign': assign_form})

Here is my html

{% extends 'todoapp/base.html' %}

{% block title %}Create a task{% endblock %}

{% block content %}
    <h2>Create a task and assign it to a user</h2>
    <form method="post">
        {% csrf_token %}
        {{ assign.as_p }}
        <br/><input type="submit" value="Assign">
        &nbsp;&nbsp;<button onclick="location.href='{%url 'dashboard' %}'" type="button">Go back</button>
    </form>
{% endblock %}

Apparently this solution works for me

class AssignTask(forms.Form):
    title = forms.CharField(max_length=200)
    description = forms.CharField(widget=forms.Textarea)
    assign_to = forms.ChoiceField(widget=forms.Select(choices=[]))

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        user_email = self.user.email.split('@')[1]
        super(AssignTask, self).__init__(*args, **kwargs)
        self.fields['assign_to'] = forms.ChoiceField(choices=[(i.email, i.email) for i in User.objects.filter(is_active=True, email__icontains=user_email)])
3
  • What do you mean it shows a list? Commented Mar 4, 2019 at 5:52
  • A list as in a column sort of field with email-ids that are selectable Commented Mar 4, 2019 at 5:55
  • what different between your solution and the solution by @Exprator? Commented Mar 4, 2019 at 7:41

1 Answer 1

1

If I understand correctly, can this code be the solution?

forms.py

class AssignTask(forms.Form):
    title = forms.CharField(max_length=200)
    description = forms.CharField(widget=forms.Textarea)
    # assign_to = forms.MultipleChoiceField(choices=[], widget=forms.CheckboxSelectMultiple, required=False)

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        user_email = self.user.email.split('@')[1]
        super(AssignTask, self).__init__(*args, **kwargs)
        # self.fields['assign_to'] = forms.MultipleChoiceField(choices=[(i.email, i.email) for i in User.objects.filter(is_active=True, email__icontains=user_email)])
        self.fields['assign_to'] = forms.ChoiceField(choices=[(i.email, i.email) for i in User.objects.filter(is_active=True, email__icontains=user_email)])

Can you see a dropdown box? And is this what you mean?

I guess you probably forgot to change inside the __init__ function.


↓Added-----

What is important is inside the __init__. The "assign_to" you declared first will be overwritten inside the __init__ section. For example, assuming that you declared "assign_to" as URLField, it will be overwritten by ChoiceField. What I am explaining is the below

forms.py

class AssignTask(forms.Form):
    title = forms.CharField(max_length=200)
    description = forms.CharField(widget=forms.Textarea)
    assign_to = forms.URLField()

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        user_email = self.user.email.split('@')[1]
        super(AssignTask, self).__init__(*args, **kwargs)
        print(self.fields)
        self.fields['assign_to'] = forms.ChoiceField(choices=[(i.email, i.email) for i in User.objects.filter(is_active=True, email__icontains=user_email)])
        print(self.fields)

Output

OrderedDict([('title', <django.forms.fields.CharField object at 0x0000000004D90C88>), ('description', <django.forms.fields.CharField object at 0x0000000004D90CC0>), ('assign_to', <django.forms.fields.URLField object at 0x0000000004D90668>)])
OrderedDict([('title', <django.forms.fields.CharField object at 0x0000000004D90C88>), ('description', <django.forms.fields.CharField object at 0x0000000004D90CC0>), ('assign_to', <django.forms.fields.ChoiceField object at 0x0000000004D906A0>)])

and a dropdown box is still displayed, isn't it?

Sign up to request clarification or add additional context in comments.

4 Comments

I have added a working solution for me. Check out the edited question
Yes, it works. What is important is inside the init. I added the reason, please check it.
Good, any other problems?
No nothing as of now. Thanks a lot for the help ! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.