Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

python - Endless select when two child models of one abstract model in annotation fields

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...