简体   繁体   中英

Django Model Field to Count Other Field

I have these models:

 
 ​class​ ​HatchingBatch​(​models​.​Model​): 
 ​    ​hatch_date​ ​=​ ​models​.​DateField​() 
 ​    ​incubator​ ​=​ ​models​.​CharField​(​max_length​=​32​) 
 ​    ​rhode_island_red​ ​=​ ​models​.​IntegerField​(​default​=​0​) 
 ​    ​barred_plymouth_rock​ ​=​ ​models​.​IntegerField​(​default​=​0​) 
 ​    ​black_australorp​ ​=​ ​models​.​IntegerField​(​default​=​0​) 
 ​     
 ​class​ ​Reservations​(​models​.​Model​): 
 ​    ​date​ ​=​ ​models​.​DateField​() 
 ​    ​hatch​ ​=​ ​models​.​ForeignKey​(​HatchingBatch​, ​related_name​=​"reservation"​, ​on_delete​=​models​.​CASCADE​) 
 ​    ​is_done​ ​=​ ​models​.​BooleanField​() 
 ​    ​is_delivery​ ​=​ ​models​.​BooleanField​() 
 ​    ​shipping_fee​ ​=​ ​models​.​IntegerField​() 
 ​    ​amount​ ​=​ ​models​.​IntegerField​()  
  
 ​
 ​class​ ​Stocks​(​models​.​Model​): 
 ​    ​hatch​ ​=​ ​models​.​OneToOneField​(​HatchingBatch​, ​on_delete​=​models​.​CASCADE​, ​related_name​=​"stock"​) 
 ​    

I want to add a field to the Stocks model that would be the number of the reserved chicks (easily done through ForeignKey) however, I also want to add a "sold" field that would correspond to the sum of the Reservations with "is_done": True

You can .annotate(…) [Django-doc] with:

from django.db.models import 

Stocks.objects.annotate(
    
)

The Stocks objects that arise from this QuerySet will have an extra attributes .reservations and .sold .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM