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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:08:25+00:00 2026-05-18T12:08:25+00:00

I have the following Django models: class Order: order_id order_date class OrderItem models.ForeignKey(Order) models.ForeignKey(Product)

  • 0

I have the following Django models:

class Order:
    order_id
    order_date
class OrderItem
    models.ForeignKey(Order)
    models.ForeignKey(Product)
    quantity
    unit_price

To explain, an order can contain multiple order items.

Now, I want to create a template which would allow the creation of an order. So in my view function I have the following:

def new_order:
    order = Order()
    OrderItemFormSet = inlineformset_factory(Order, OrderItem, extra=2)
    formset = OrderItemFormSet()
    cntx = {'formset': formset, 'order':order}
    return render_to_response('some_page.html', cntx)

In the template itself, I iterate over the forms in the formset and all seems to work meaning I am able to create an order with multiple order items.

Now, I have created a new model called Product. The new Product model will be part of the OrderItem model. So we order item contains a product along with other metrics like quantity, unit price etc.

class Product:
    product_code
    product_name

Next, I updated the OrderItem model with the following line:

product = models.ForeignKey(Product)

The problem I am facing is how to handle the above scenario in the template. Before adding the Product model, the for loop which iterates over the formset is like this:

{% for form in formset %}
   <td> {{form.quantity}} </td>
   ....
{%endfor%}

Now, ideally, I thought I could this:

{% for form in formset %}
  <td> {{form.quantity}} </td>
  <td> {{form.product.code}} </td>
  ....

But the above does not work. Basically, what I want to achieve is to be able to add the product code while creating the order items for an order. Upon save, the order item should be saved with the corresponding product id of the product code entered by the user.

Any insight will be appreciated.

Thanks…

  • 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-18T12:08:26+00:00Added an answer on May 18, 2026 at 12:08 pm

    You can’t have multiply nested formsets. But you don’t need them here. All you really want is for the orderitem form to have a product field, which is a select box of all the available products and their codes. And you get that automatically, as long as you define the __unicode__ method of the Product model to return the product code, and then use form.product in the template.

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

Sidebar

Related Questions

I have the following models: class Order_type(models.Model): description = models.CharField() class Order(models.Model): type= models.ForeignKey(Order_type)
I have the following: from django.contrib.auth.models import User class ClientDetails(models.Model): created_by = models.ForeignKey(User) ...
Let's say I have the following Django model: class Info(models.Model): instrument = models.ForeignKey('Instrument') date
I have the following model in Django: class Bout (models.Model): fighter_1 = models.ForeignKey(Fighter, related_name=bout_fighter_1)
I have the following abstract Django models: class Food(models.Model): name = models.CharField(max_length=100) class Meta:
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have the following models in my Django app. How can I from the
Suppose I have the following Event model: from django.db import models import datetime class
I have the following Django models: - class Company(models.Model): name = models.CharField(max_length=50) is_active =
Let's say I have the following Django models: class A(models.Model): keywords = models.ManyToManyField(Keyword) 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.