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 6388825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:19:39+00:00 2026-05-25T03:19:39+00:00

I have a problem saving a ModelForm when using a foreign key that doesn’t

  • 0

I have a problem saving a ModelForm when using a foreign key that doesn’t “point” to the primary key of it’s related table (legacy schema woes) I am using to_field= for my Foregin Key so that it will be related to a key that is not exactly a key. My ModelForm foreignkey uses a ModelChoiceField with a queryset and a widget of HiddenInput() because the default rendering takes 2 minutes. I get an invalid choice when I try to save because the queryset returns the primary key as the option value when returning the related object (Checkin). How can I still use ModelChoiceField with this setup? My basic schema below.

class Checkin(models.Model):
    sampleid = models.CharField(unique=True, max_length=255, db_column='SampleID', primary_key=True)
    #shortsampleid is the field that is sometimes used as a sort of pk. 
    shortsampleid = models.IntegerField(unique=True, db_column='ShortSampleID') 
    company = models.CharField(max_length=765, db_column='Company', blank=True)
    ...

class Tblshipmentstore(models.Model):
    shortsampleid = models.ForeignKey(Checkin, to_field='shortsampleid', db_column='ShortSampleID')
    shipmentitem = models.CharField(max_length=765, db_column='ShipmentItem', blank=True)
    shipdate = models.DateField(null=True, db_column='ShipDate', blank=True)
    ...

class TblShipmentstoreForm(ModelForm):
    shortsampleid = forms.ModelChoiceField(queryset=Checkin.objects.all(), widget=forms.HiddenInput());

   class Meta:
       model = 'Tblshipmentstore'
  • 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-05-25T03:19:39+00:00Added an answer on May 25, 2026 at 3:19 am

    ModelChoiceField has an undocumented to_field_name parameter that you can pass in at construction time, which makes it use that field instead of the primary key.

    It sounds like you want to use this (untested):

    shortsampleid = forms.ModelChoiceField(
        queryset=Checkin.objects.all(),
        to_field_name = 'shortsampleid',
        widget=forms.HiddenInput());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using CakePHP framework with MySQL database and I have problem in saving
I have a problem with saving a huge amount of records to database using
I have having a problem saving the sign up form using Devise. I am
We have a weird intermittent problem with saving from Word 2007 to our SharePoint
We have a problem with the Euro character when saving and retrieving it from
I have problem in some JavaScript that I am writing where the Switch statement
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
i have a problem with saving something and am officially out of ideas. what
I have a problem that I don't know how to solve. I have have
I have problem with saving object to database with NHibernate. Program throws no error

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.