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

  • SEARCH
  • Home
  • 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 267953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:37:37+00:00 2026-05-11T23:37:37+00:00

This problem is very strange and I’m hoping someone can help me. For the

  • 0

This problem is very strange and I’m hoping someone can help me. For the sake of argument, I have a Author model with ForeignKey relationship to the Book model. When I display an author, I would like to have a ChoiceField that ONLY displays the books associated with that author. As such, I override the AuthorForm.init() method and I create a List of choices (tuples) based upon a query that filters books based upon the author ID. The tuple is a composite of the book ID and the book name (i.e., (1, ‘Moby Dick’)). Those “choices” are then assigned to the ModelForm’s choices attribute.

When the form renders in the template, the ChoiceField is properly displayed, listing only those books associated with that author.

This is where things get weird.

When I save the form, I receive a ValueError (Cannot assign “u’1′”:Author.book” must be a Book instance). This error makes sense due to the FK relationship. However, if I add a “print” statement to the code, make no other changes, and then save the record, it works. The ValueError magically disappears. I’ve tried this a number of times, ensuring I haven’t inadvertently made another change, and it works each time.

Does anyone know what’s going on here?

  • 1 1 Answer
  • 2 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-05-11T23:37:38+00:00Added an answer on May 11, 2026 at 11:37 pm

    Not quite sure what you are doing wrong, but it is best to just modify the queryset:

    class ClientForm(forms.ModelForm):
    
        def __init__(self, *args, **kwargs):
            self.affiliate = kwargs.pop('affiliate')
            super(ClientForm, self).__init__(*args, **kwargs)
            self.fields["referral"].queryset = Referral.objects.filter(affiliate = self.affiliate)
    
        class Meta:
            model = Client
    

    The above is straight out of one my projects and it works perfectly to only show the Referral objects related to the passed affiliate:

    form = ClientForm(affiliate=request.affiliate)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very confused and am hoping someone might have had this problem and be
This is a very strange problem, and I just hope that I can clearly
Starting from Android 3.2 I have this strange problem. It's very easy to reproduce:
I have a very strange problem with facebook OG. https://fit.upjet.com/en/pages/news/270/ - this is a
I have this very strange problem on a big flex app where it would
I have a very strange problem with wordpress. I have this kind of pages:
This problem is very strange, maybe I have missed some small detail, since I'm
Possible Duplicate: Can main function call itself in C++? I found this problem very
This is a very strange problem. I've got a rails app in which I
I am facing a very strange problem. I have a grid and when i

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.