0

I have this code in a django test:

stockitem_retailer = StockItem.objects.filter(retailer=test_retailer).first()
test_subcategory = SubCategory.objects.create(category=test_category, name="outdoors")
stockitem_retailer.product.subcategory = test_subcategory
stockitem_retailer.save()
pdb.set_trace()

self.assertTrue(StockItem.objects.filter(product__subcategory=test_subcategory, retailer=test_retailer).exists())

The code above gets a StockItem object with retailer=test_retailer, then id adds a created subcategory "outdoors" to the stockitem.product.subcategory relation. Why then the test does not pass?

This is what I get from pdb:

(Pdb) test_subcategory
<SubCategory: outdoors>
(Pdb) test_retailer
<Retailer: mi-super>
(Pdb) stockitem_retailer.product.subcategory
<SubCategory: outdoors>
(Pdb) stockitem_retailer.retailer
<Retailer: mi-super>
(Pdb) self.assertTrue(StockItem.objects.filter(product__subcategory=test_subcategory, retailer=test_retailer).exists())
*** AssertionError: False is not true

I've being looking at this code for hours now, I can't se the bug. Please help.

kgui
  • 4,015
  • 5
  • 41
  • 53
Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180

1 Answers1

1

You set test_subcategory on stockitem_retailer.product, but you never save stockitem_retailer.product, you only save stockitem_retailer.

Simply add stockitem_retailer.product.save() to save the product.

knbk
  • 52,111
  • 9
  • 124
  • 122