How to make django form dropdown shows items created by the author only not by all the users?

I have created two models Topics and entries. User can create topic and also create the entries for each topic they created.

In Create Topic Entry class, the fields 'topic' showing all topics created by all users in the form dropdown menu.

However, I just want the form dropdown shows topics created by the author only so that user can only create the entry for their topic but not others topic. How to do that?

models.py:
    
    class Topic(models.Model):
        '''A topic that user will add the content about.'''
        title = models.CharField(max_length=200, unique=True)
        author = models.ForeignKey(User, on_delete=models.CASCADE)
    
    
    class Entries(models.Model):
        '''Entries and Topic will have many to one relationship'''
        topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
        name = models.CharField(max_length=100)
        text = models.TextField()
        entries_image = models.ImageField(default= 'default.jpg', upload_to ='images')
        date_added = models.DateTimeField(auto_now_add=True)
        author = models.ForeignKey(User, on_delete=models.CASCADE)

views.py
    
    class CreateTopic(LoginRequiredMixin, CreateView):
        model = Topic
        fields = ['title']
    
    
        def form_valid(self, form):
            form.instance.author = self.request.user
            return super().form_valid(form)  
    
    
    class CreateTopicEntry(LoginRequiredMixin, CreateView):
        model = Entries
        fields = ['topic', 'name','text','entries_image']
    
    
        def form_valid(self, form):
            form.instance.author = self.request.user
            return super().form_valid(form)


Topic_form.html:
    
    <div>
        <form method="POST">
            {% csrf_token %}
                {% if object  %}
                    <h4 class="mb-4">Update your Topic &#128071</h4>
                    {{ form|crispy }}
                {% else %}
                    <h4 class="mb-4">Create your new Topic &#128071</h4>
                    {{ form|crispy }}
                {% endif %}
            <div class="form-group">
                <button class="btn btn-light" type="submit">Submit</button>
            </div>
        </form>
    </div>
    
entries_form.html:
    
        <div class="mt-5">
            <form method="POST" enctype="multipart/form-data">
                {% csrf_token %}
                    {% if object  %}
                        <h4 class="mb-4">Update your Entry &#128071</h4>
                            {{ form|crispy }}
                        {% else %}
                        <h4 class="mb-4">Create your new Entry &#128071</h4>
                            {{ form|crispy }}
                        {% endif %}
                    <div class="form-group">
                        <button class="btn btn-light" type="submit">Submit</button>
                    </div>
            </form>
        </div>

You just need to override the form's queryset in the CreateTopicEntry's get_form method, like this:

class CreateTopicEntry(LoginRequiredMixin, CreateView):
    # Keep the code you already have 
    # ...

    def get_form(self, *args, **kwargs):
        form = super().get_form(*args, **kwargs)
        # Filter the queryset to include only topics by current user
        form.fields['topic'].queryset = Topic.objects.filter(author=self.request.user)
        return form
Вернуться на верх