Django - How to filter dropdown based on user id?
<p>I'm not sure how to filter dropdown based on user id.</p>

<p>Not I want for user id 2.</p>

<p><a href="http://i.stack.imgur.com/iJ4Zm.png" rel="nofollow"><img src="http://i.stack.imgur.com/iJ4Zm.png" alt="enter image description here"></a></p>

<p>I want exactly like this for user id 2.</p>

<p><a href="http://i.stack.imgur.com/1XF7A.png" rel="nofollow"><img src="http://i.stack.imgur.com/1XF7A.png" alt="enter image description here"></a></p>

<p>Model</p>

<pre><code>@python_2_unicode_compatible  # only if you need to support Python 2
class PredefinedMessage(models.Model):
    user = models.ForeignKey(User)
    list_name = models.CharField(max_length=50)
    list_description = models.CharField(max_length=50)

    def __str__(self):
        return self.list_name

class PredefinedMessageDetail(models.Model):
    predefined_message_detail = models.ForeignKey(PredefinedMessage)
    message = models.CharField(max_length=5000)
</code></pre>

<p>View</p>

<pre><code>class PredefinedMessageDetailForm(ModelForm):
    class Meta:
        model = PredefinedMessageDetail
        fields = ['predefined_message_detail', 'message']
        exclude = ('user',)

def predefined_message_detail_update(request, pk, template_name='predefined-message/predefined_message_detail_form.html'):
    if not request.user.is_authenticated():
        return redirect('home')
    predefined_message_detail = get_object_or_404(PredefinedMessageDetail, pk=pk)
    form = PredefinedMessageDetailForm(request.POST or None, instance=predefined_message_detail)
    if form.is_valid():
        form.save()
        return redirect('predefined_message_list')
    return render(request, template_name, {'form':form})
</code></pre>

<p>html file</p>

<pre><code>{% extends "base.html" %}
{% load i18n %}

{% block content %}
&lt;form method="post"&gt;{% csrf_token %}
    {{ form.as_p }}
    &lt;input type="submit" value="Submit" /&gt;
&lt;/form&gt;
{% endblock %}
</code></pre>

<p>You can do it in view itself using</p>

<pre><code>form = PredefinedMessageDetailForm(request.POST or None, instance=predefined_message_detail) 
form.fields["predefined_message_detail"].queryset= PredefinedMessage.objects.filter(user=request.user)
</code></pre>

<p>But filtering happens based on <code>request.user</code> so it should be logged in.Consider that also. Hope this helps</p>

python django