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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:08:56+00:00 2026-05-26T01:08:56+00:00

My models look like this: class Article(models.Model): name = models.CharField(max_length=50) description = models.TextField() price

  • 0

My models look like this:

class Article(models.Model):
name = models.CharField(max_length=50)
description = models.TextField()
price = models.FloatField()

def __unicode__(self):
    return "%s - %s" % (self.name, self.price)

class Order(models.Model):
client = models.ForeignKey(User)
articles = models.ManyToManyField(Article)
total = models.FloatField(editable=False, blank=True, default=0)

def __unicode__(self):
    return "%s - %s" % (self.client.username, self.total)

def save(self):
    for a in self.articles:
        self.total += a.price
    super(Order, self).save()

And I try to calculate the total price in save(). If I try to save an order via the admin, I get

‘Order’ instance needs to have a primary key value before a many-to-many relationship can be used

If I call

super(Order, self).save()

before calculating the sum, I get

‘ManyRelatedManager’ object is not iterable
.

I don’t know to do… I thought about calculating total in the view, but I would like to store it in my database.

How to I iterate over all the articles in the Order-Model?

Thanks in advance 🙂

  • 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-26T01:08:57+00:00Added an answer on May 26, 2026 at 1:08 am

    I’d replace your total field with a property:

    class Order():
        ....
        @property
        def total(self):
            return self.articles.aggregate(Sum('price'))['price__sum']
    

    the reason is: when you add items to collection your object is not saved again, so it wouldn’t update your counter. With this property you’ll be sure to always have correct data.

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

Sidebar

Related Questions

My models look like this: class Category(models.Model): name = models.CharField(_('name'), max_length=50) class Product(models.Model): name
This is what part of my models look like: class Character(models.Model): name = models.CharFied()
I've got a model similar to this: class Person(models.Model): name = models.CharField(max_length=40) birthday =
I've got a set of models that look like this: class Page(models.Model): title =
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I have a django model and model form that look like this: -models.py class
I have the following structure: class FeatureType(models.Model): type = models.CharField(max_length=20) def __unicode__(self): return self.type
If my models look like this: (app/models/letter.rb) class Letter < ActiveRecord::Base def cyrilic_equivilent #
I've got models like this: class PledgeItem(models.Model): title = models.CharField(...) usd_amount = models.DecimalField(...) 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.