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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:52:31+00:00 2026-05-20T14:52:31+00:00

Hi I have a domain model, used in Django app, which I’d like to

  • 0

Hi
I have a domain model, used in Django app, which I’d like to present on a single form. I’ve created my application with custom ModelForms (not much changes, some fields excluded etc). The model’s dependencies are as follows:

Complaint
   \
    .--- CarInfo
    .--- Customer

My view function looks like this:

def make(request):
  if request.method == 'POST':
    parameters = copy.copy(request.POST)
    complaint = Complaint()
    carInfo = CarInfo()
    customer = Customer()

    customer_form = CustomerForm(parameters, instance=customer)
    carInfo_form = CarInfoForm(parameters, instance=carInfo)
    parameters['complaint_date'] = get_current_date()
    parameters['customer'] = 1 # dummy value to allow validation success
    parameters['car_info'] = 1 # dummy value to allow validation success
    form = ComplaintForm(parameters, instance=complaint)
    if form.is_valid() and customer_form.is_valid() and carInfo_form.is_valid():
      carInfo_form.save()
      customer_form.save()
      parameters['customer'] = customer.id
      parameters['car_info'] = carInfo.id
      form = ComplaintForm(parameters, instance=complaint)
      form.save()
      return index(request)
  else:
    form = ComplaintForm()
    carInfo_form = CarInfoForm()
    customer_form = CustomerForm()
  return render_to_response('complaints/make_complaint.html', {'complaint_form' : form, 'customer_form' : customer_form, 'carInfo' : carInfo_form})

I don’t like this approach too much, moreover it doesn’t work in all environments -thou I haven’t found the reason for it not working. I’ve been looking into fixing this code a bit and found something like inline formset (http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets). This solution seems ok, but since my forms are custom tailored I can use it.

Perhaps someone could offer me some advice on how to properly solve such case. Cleaner solutions are much appreciated.

EDITED
There is a case for me, where this solutions just doesn’t work. Despite of setting dummy values on foreign keys, when I call is_valid() I get FALSE, with error message saying that these fields are not set. I’m observing this problem with django 1.2.5 – it occurs on server I intent to run this app, however my laptop (also django 1.2.5) doesn’t have this problem.

  • 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-20T14:52:31+00:00Added an answer on May 20, 2026 at 2:52 pm

    You can change your Complaint model’s complaint_date to something like this

    complaint_date = models.DateField(default=datetime.date.today())

    that way you can get rid of

    parameters['complaint_date'] = get_current_date()

    As for your multiform view you can use an unbound forms for you desired behavior.

    By excluding the fk to the car and customer on the complaint form, the forms should validate. Check the .is_valid() of all 3 forms at the same time and then save the 2 forms that your complaint object is dependent on, create the complaint object with out commit to database (commit=False), add the id’s of the customer and car to that object, then save.

    in your view.

    def make(request):
        if request.method == 'POST':
            customer_form = CustomerForm(request.POST)
            carInfo_form = CarInfoForm(request.POST)
            form = ComplaintForm(request.POST)
    
            if form.is_valid() and customer_form.is_valid() and carInfo_form.is_valid():
                car_instance = carInfo_form.save()
                customer_instance = customer_form.save()
    
                complaint_instance = form.save(commit=False)
                complaint_instance.car_info = car_instance
                complaint_instance.customer_info = customer_instance          
                complaint_instance.save()
    
                return index(request)
        else:
            form = ComplaintForm()
            carInfo_form = CarInfoForm()
            customer_form = CustomerForm()
    
        context = { 'complaint_form' : form,
                    'customer_form' : customer_form, 
                    'carInfo' : carInfo_form,
                  }
        return render_to_response('complaints/make_complaint.html', context, context_instance=RequestContext(request))
    

    edit:

    models look like this:

    class CarInfo(models.Model):
        some_car_info = models.CharField()
    
    class Customer(models.Model):
        some_customer_info = models.CharField()
    
    class Complaint(models.Model):
        car_info = models.ForeignKey(CarInfo)
        customer_info = models.ForeignKey(Customer)
        some_complaint_info = models.CharField()
    

    forms.py should look like this:

    class CarInfoForm(forms.ModelForm):
        class Meta:
            model = CarInfo
    
    class CustomerForm(forms.ModelForm):
        class Meta:
            model = Customer
    
    class ComplaintForm(forms.ModelForm):
        class Meta:
            model = Complaint
            exclude = ('car_info', 'customer_info',) # or include = ('some_complaint_info',)
    

    Lets walk through the view I wrote out above:
    form in view docs

    • On first pass, there is no request.method so we create 3 unbound forms.

      else:
          form = ComplaintForm()
          carInfo_form = CarInfoForm()
          customer_form = CustomerForm()
      
    • these forms are passed to the template and rendered.

    • When the view is called again with request.method == “POST” evaluating true, we create the 3 bound form instances using the data from our request.POST.

      if request.method == 'POST':
          customer_form = CustomerForm(request.POST)
          carInfo_form = CarInfoForm(request.POST)
          form = ComplaintForm(request.POST)
      
    • Next we call the .is_valid() method on each form. In our example because we excluded the ‘customer_info’ and ‘car_info’ foreign key fields in our complaint modelform, each form is only checking to see that the char input field validates.

    • If validation all passes then we can start saving our forms to models and this where we need to be careful about populating our complaint’s required fk’s:

          if form.is_valid() and customer_form.is_valid() and carInfo_form.is_valid():
              car_instance = carInfo_form.save()
              customer_instance = customer_form.save()
      
    • With those 2 forms we can call the .save() as usual. We will however assign the return value to car_instance and customer_instance. These will contain the instances of the CarInfo and Customer models we just created using the .save() method on the form.

    • Next, using the commit=False argument in the .save() method, we are able to create an object from the bound form (containing the request.POST data) and not save it to the database.

              complaint_instance = form.save(commit=False)
              complaint_instance.car_info = car_instance
              complaint_instance.customer_info = customer_instance          
              complaint_instance.save()
      
    • To make this clearer, you could also have created a new Complaint object like this:

      complaint_info = form.cleaned_data.get('some_complaint_info')
      complaint_instance = Complaint(car_info=car_instance, customer_info=customer_instance, some_complaint_info=some_complaint_info)
      complaint_instance.save()
      
    • render

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

Sidebar

Related Questions

We have a custom LocalizedString type used in our domain model. We want to
In Grails 1.3.7, I have a domain model like this: abstract class A {
I have a class in my domain model root that looks like this: namespace
I have two tables that are considered a single entity in my domain model.
I have a Spring MVC application trying to use a rich domain model, with
I'm building a WPF application and while creating my domain model I used ObservableCollection<T>
We have implemented our domain model using the accountability pattern, which we are trying
Suppose you have an application that utilizes the domain-model pattern, DDD and lots of
I have a partial view which contains the following snippet: @model Mbrrace.Domain.MbrraceForm <div class=row>
I used to design my application around anemic domain model, so I had many

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.