Skip to content

Commit

Permalink
Improved Product.__str__() methods error handling
Browse files Browse the repository at this point in the history
Now the case that a Price that is not associated to a Product will work. This can happen when stripe.Price is used to create a new object and then order of webhooks on the local can cause this corner case to occur
  • Loading branch information
arnav13081994 authored and jleclanche committed Nov 26, 2021
1 parent b290169 commit cb0c91a
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions djstripe/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,14 @@ class Product(StripeModel):
def __str__(self):
# 1 product can have 1 or more than 1 related price
price_qs = Price.objects.filter(product__id=self.id)
price_count = price_qs.count()

if price_qs.count() > 1:
return f"{self.name} ({price_qs.count()} prices)"
else:
if price_count > 1:
return f"{self.name} ({price_count} prices)"
elif price_count == 1:
return f"{self.name} ({price_qs[0].human_readable_price})"
else:
return self.name


class Customer(StripeModel):
Expand Down

0 comments on commit cb0c91a

Please sign in to comment.