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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:29:03+00:00 2026-06-18T09:29:03+00:00

I have a model FooModel with 2 fields with a default value ( default=xxx

  • 0

I have a model FooModel with 2 fields with a default value (default=xxx) and marked as blank (blank=True), I created a ModelForm(django.forms.ModelForm) using FooModel and now I want to save it after submission, so in my view I have the following code:

f = FooForm(request.POST)

if f.is_valid():
  f.save()

the problem is that in this way I get a violation exception from the database because the fields that are not rendered in the html form are not automatically inherited in the FooForm instance as I would expect… how can I include fields from the original model which should not be displayed to the user? (I don’t want to render them as hidden fields!)

So far I tried 2 approaches, both failed…

  1. Specify instance in the FooForm constructor (f = FooForm(request.POST, instance=FooModel()))

  2. Create an instance of a FooModel and manually assign the auto-generated values to the form’s data:

    i = FooModel()
    
    f.data.fieldA = i.fieldA
    
    f.data.fieldB = i.fieldB
    

UPDATE:

by reading the django documentation more accurately, I solved in this way:

if f.is_valid():
  formModel = f.save(commit=False)
  foo = FooModel()
  formModel.fieldA = foo.fieldA
  formModel.fieldB = foo.fieldB
  formModel.save()

but, to be honest, I’m not satisfied… I would like to abstract out the addition of those fields… perhaps by using a custom decorator… something like:

f = MissingFieldsDecorator(FooForm(request.POST))
f.save()
  • 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-18T09:29:04+00:00Added an answer on June 18, 2026 at 9:29 am

    Also try the approach mentioned here https://docs.djangoproject.com/en/1.4/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form

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

Sidebar

Related Questions

I have created a model which has foreign key to the django.contrib.auth.models User model.
I have model that looks like this: class Category(models.Model): parentCategory = models.ForeignKey('self', blank=True, null=True,
I have model like this: class File(models.Model): name = models.CharField(max_length=45) description = models.CharField(max_length=100, blank=True)
I have model created field configurated as follows: created = models.DateTimeField(auto_now_add=True) In JSON fixtures
Let's say I have some model objects that resemble this: public class FooModel {
I have model: [XmlRoot(ElementName = event, IsNullable=true)] public class Event { public int id
I have model, and when i write into it, i'm using standart Model.create!(params[:somesymbol]) However
I have model with following field. date = models.DateTimeField(auto_now_add=True) When querying such model i`d
im have model Category. class Category < ActiveRecord::Base has_ancestry :cache_depth => true, :depth_cache_column =>
I'm writing an MVC2 app using DataAnnotations. I have a following Model: public class

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.