Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:00:01+00:00 2026-06-07T05:00:01+00:00

Possible Duplicate: How do I filter ForeignKey choices in a Django ModelForm? Say I

  • 0

Possible Duplicate:
How do I filter ForeignKey choices in a Django ModelForm?

Say I have some models that look like this:

from django.db import models
from django.contrib.auth.models import User

class Author(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    owner = models.ForeignKey(User)

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    owner = models.ForeignKey(User)

If I create a ModelForm for the Book model, users are allowed to select from all authors, not just the ones they own. How would I create a ModelForm for the Book model that lets the user select only authors that s/he owns?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:00:02+00:00Added an answer on June 7, 2026 at 5:00 am

    You can override the model form’s __init__ method and restrict the queryset for the author model choice field.

    class BookForm(forms.ModelForm):
    
        class Meta:
            model = Book
            # exclude the owner and set it in the view
            exclude = ('owner',) 
    
        def __init__(self, *args, **kwargs):
            super(BookForm, self).__init__(*args, **kwargs)
            if self.instance.owner:
                self.fields['author'].queryset = Author.objects.filter(owner=self.instance.owner)
    

    In your view, make sure that you instantiate the form with an instance that already has the owner set. For example to add a book, it would look something like this:

    def add_book(request):
        book = Book(owner=request.owner)
        form = BookForm(data=request.POST or None, instance=book)
        if form.is_valid():
            book.save()
            return HttpResponseRedirect('/success-url/')
        return render(request, "my_template.html", {'form': form})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to do this? I have some filters set in my source
Is it possible to stack multiple filter attributes? For instance I have this attribute
Possible Duplicate: How do you implement a good profanity filter? I have to take
Possible Duplicate: PHP code to remove everything but numbers i have a loop that
Possible Duplicate: JavaScript Array Delete Elements So, in javascript, I have this set up:
Possible Duplicate: PHP: Filter array I have a PHP array containing strings of random
Possible Duplicate: How do you implement a good profanity filter? I have an iPhone
Possible Duplicate: “bad words” filter In my web application i have a section which
Possible Duplicate: How do you implement a good profanity filter? I am developing a
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.