There are two models, which inherit from one abstract model
class ProductToStock(models.Model):
product = None
stock = None
qty = models.PositiveSmallIntegerField(db_index=True)
price = models.DecimalField(decimal_places=2, max_digits=8, db_index=True)
class Meta:
abstract = True
class TireToStock(ProductToStock):
product = models.ForeignKey('Tire', related_name='offers', on_delete=models.CASCADE)
stock = models.ForeignKey('providers.Stock', related_name='available_tires', on_delete=models.CASCADE)
class WheelToStock(ProductToStock):
product = models.ForeignKey('Wheel', related_name='offers', on_delete=models.CASCADE)
stock = models.ForeignKey('providers.Stock', related_name='available_wheels', on_delete=models.CASCADE)
And there is a stock model:
class Stock(models.Model):
name = models.CharField(max_length=200)
I want to select all stocks and get count of tires and wheels on each of them. If I get only one of my counts it works fine:
Stock.objects.annotate(
tires_count=Count('available_tires')
)
But if I get both counts, I get endless recursive select:
Stock.objects.annotate(
tires_count=Count('available_tires'),
wheels_count=Count('available_wheels'),
)
How can I select both counts in one query?
question from:
https://stackoverflow.com/questions/65907424/endless-select-when-two-child-models-of-one-abstract-model-in-annotation-fields 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…