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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:39:22+00:00 2026-05-11T20:39:22+00:00

I have a this model: class Fleet(models.Model): company = models.ForeignKey(Company, editable=False) aircraft = models.ForeignKey(Aircraft)

  • 0

I have a this model:

class Fleet(models.Model):
    company     =   models.ForeignKey("Company", editable=False)
    aircraft    =   models.ForeignKey("Aircraft")
    size        =   models.IntegerField(default=1)
    description =   models.TextField(blank=True)

    def __unicode__(self):
        return u"%s" % (self.aircraft, )

And then a form based on this model:

class FleetForm(ModelForm):
    class Meta:
        model = Fleet
        exclude = ('company', )

When I use this form in a template, the “company” field is not added, which is expected. But that field is required as blank != True.

The way I use this form, the company attribute will always be known in the view function, as it’s passed from the URL. How can I add the company to the form in the view function before I save it?

Here is my view:

def new_fleet(request, company_id):
    from forms import FleetForm

    company = Company.objects.get(pk=company_id)

    if request.method == "POST":
        form = FleetForm(request.POST,)

        form.company = company            #doesn't work

        form = form.save(commit=False)    #can't do this because the form 
        form.company = company            #doesn't validate (company is not set)

        if not form.errors:
            form.save()

    else:
        fleet = Fleet(company=company)    #pointless because the company widget
        form = FleetForm(instance=fleet)  #isn't created but eh whatever
  • 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-11T20:39:22+00:00Added an answer on May 11, 2026 at 8:39 pm

    There are two ways to solve this issue:

    Instantiate your model with initial values for the missing, but required fields:

    company = Company.objects.get(pk=company_id)
    fleet = Fleet(company=company)
    form = FleetForm(request.POST, instance=fleet)
    new_fleet = form.save()
    

    Use save(commit=False) and manually set any extra required fields:

    company = Company.objects.get(pk=company_id)
    form = FleetForm(request.POST)
    fleet = form.save(commit=False)
    fleet.company = company
    new_fleet = fleet.save()
    

    See the note in this section of the ModelForm API documentation for more details.

    By the way, either editable=False or exclude is enough to remove a field from a ModelForm; you don’t need both.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this model: class blog(models.Model): user = models.ForeignKey(User) mail = models.EmailField(max_length=60, null=False, blank=False)
I have this model class Vacancy(models.Model): user = models.ForeignKey(User, null=True, blank=True, default = None)
I have this model: class UserProfile(models.Model): (UserProfile description) user = models.ForeignKey(User) image = models.ImageField(upload_to=upload_to)
assume I have this little model: class Deal(models.Model): purchases = models.IntegerField(default=0)#amount of purchases so
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
Imagine you have this model: class Category(models.Model): node_id = models.IntegerField(primary_key = True) type_id =
I have the following model: class A(models.Model): name = models.CharField(max_length=50) content_type = models.ForeignKey(ContentType) This
I have this model: class Category(models.Model): name = models.CharField() description = models.CharField(blank=True) parent =
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
Okay so I have this mode: class Posts(db.Model): rand1 = db.FloatProperty() #other models here

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.