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

  • Home
  • SEARCH
  • 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 9202985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:22:41+00:00 2026-06-17T23:22:41+00:00

I have defined two models for retailers and items in their stock. Product is

  • 0

I have defined two models for retailers and items in their stock. Product is defined in another app. I defined two model methods to get and add stock items. Here is the relevant part of the code:

class Retailer(models.Model):
    name = models.CharField(max_length=100)
    @property
    def stock_items(self):
        return StockItem.objects.filter(retailer__id=F('id'))

    def add_stock_item(self, product_id):
        try:
            print "Checking if it's already in stock"
            self.stock_items.get(product__id=product_id)
            print "It's already in stock"
        except ObjectDoesNotExist:
            try:
                print "Adding item to stock"
                product = Product.objects.get(pk=product_id)
                StockItem.objects.create(retailer=self, product=product)
                print "Added to stock"
            except Product.DoesNotExist:
                print "Such product doesn't exist"
    def __unicode__(self):
        return self.name


StockItem(models.Model):
    retailer = models.ForeignKey(Retailer)
    product = models.ForeignKey(Product)
    def __unicode__(self):
        return "%s - %s %s" % (self.retailer, self.product.brand, self.product)

When I want to use these model methods, something weird happens. They stop working correctly after adding the first item (In these examples, product 1 is 16 GB iPhone and Product 2 is 32 GB iPhone).

First let’s add some products to our stock:

>>> r = Retailer.objects.get(pk=1)
>>> r.stock_items
[]
>>> r.add_stock_item(1)
Checking if it's already in stock
Adding item to stock
Added to stock
>>> r.add_stock_item(2)
Checking if it's already in stock
Adding item to stock
Added to stock

So far so good. Now let’s try adding products again to see how it handles errors:

>>> r.add_stock_item(1)
Checking if it's already in stock
It's already in stock
>>> r.add_stock_item(2)
Checking if it's already in stock
Adding item to stock
Added to stock

What? Why did it add product 2 again. It was supposed to show a message similar to product 1. Let’s see our stock:

>>> r.stock_items
[<StockItem: hh - Apple iPhone 4S 16GB>]

What happened to product 2? Did it fail to add it to database?

[<StockItem: hh - Apple iPhone 4S 16GB>, <StockItem: hh - Apple iPhone 4S 32GB>, <StockItem: hh - Apple iPhone 4S 32GB>]

Apparently not. It was added to database, but somehow our program fails to check it correctly. Only the first product added to stock is shown by calling r.stock_items. Restarting shell doesn’t change the situation either, so I guess the reason couldn’t be because of when the function is evaluated.

Why does this happen and how can I fix it?

  • 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-17T23:22:42+00:00Added an answer on June 17, 2026 at 11:22 pm

    Try to remove @property and change the method to this:

    def stock_items(self):
        return self.stockitem_set.all()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models defined in Ember and also in a Rails application: App.Library
I have two models: Play and PlayParticipant , defined (in part) as: class PlayParticipant(models.Model):
So I have two models, a Ranking model and a UserRanking model. The app
In a Rails 3.2 app I have defined two methods that perform a calculation
I have two models defined loosely like this: class InformationUnit(models.Model): username = models.CharField(max_length=255) project
I have two models, Category and Point . The associations are defined as: Category
In this situation I have two models, Comment and Score. The relationship is defined
I have defined two endpoints in my App.Config file as <system.serviceModel> <services> <service name=HostDirectAddress.ITestService
I have two models : class A extends Model { public bool active =
I want to have constat defined in two models, but I don't want to

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.