djagngo 获取所有带有条件的 .objects.filter()

发布于 2025-01-11 10:29:20 字数 1589 浏览 0 评论 0原文

我有一个包含许多列的表 Batch


Column  Type    Comment
id  bigint(20) Auto Increment   
start_date  datetime(6) NULL    
acerage double  
batch_health    int(11) NULL    
stage   varchar(100) NULL   
expected_delivery_date  datetime(6) NULL    
current_pdd double NULL 
historic_pdd    double NULL 
current_gdd double NULL 
historic_gdd    double NULL 
sub_farmer_id   int(10) unsigned NULL   
batch_status    varchar(100)    
commodity_id    int(10) unsigned NULL   
commodity_variety_id    int(10) unsigned NULL   
farm_id bigint(20) NULL 
created_at  datetime(6) 
created_by_id   int(10) unsigned NULL   
updated_at  datetime(6) 
updated_by_id   int(10) unsigned NULL   
actual_produce  double NULL 
actual_yield_per_acre   double NULL 
expected_produce    double NULL 
historical_yield_per_acre   double NULL 
sop_adherence   double NULL 
end_date    datetime(6) NULL    
batch_median_health int(10) unsigned NULL   
batch_name  varchar(200) NULL   
commodity_name  varchar(200) NULL   
pending_tasks   int(10) unsigned NULL   
pest_attack varchar(500) NULL   

当我尝试从该表获取对象时,

Batch.objects.filter(farm_id = farm_id, batch_status='completed')

,就像已经定义了 farm_id 变量 一样 那么我应该该批次的所有数据列 但我没有得到所有的列,我只得到一个批次名称作为输出,

 (2706) Papaya | 2021-03-18
 (2707) Papaya | 2021-03-18

如何获取 dict 中的所有列? 这样我就可以在batchyield的过滤器中使用它

batch_id Is a foreign key in batchyield which is batch table ID

并获取batchyield的所有数据 经过

Batchyield.objects.filter(batch_id=batch_id)

I Have a table Batch with many columns


Column  Type    Comment
id  bigint(20) Auto Increment   
start_date  datetime(6) NULL    
acerage double  
batch_health    int(11) NULL    
stage   varchar(100) NULL   
expected_delivery_date  datetime(6) NULL    
current_pdd double NULL 
historic_pdd    double NULL 
current_gdd double NULL 
historic_gdd    double NULL 
sub_farmer_id   int(10) unsigned NULL   
batch_status    varchar(100)    
commodity_id    int(10) unsigned NULL   
commodity_variety_id    int(10) unsigned NULL   
farm_id bigint(20) NULL 
created_at  datetime(6) 
created_by_id   int(10) unsigned NULL   
updated_at  datetime(6) 
updated_by_id   int(10) unsigned NULL   
actual_produce  double NULL 
actual_yield_per_acre   double NULL 
expected_produce    double NULL 
historical_yield_per_acre   double NULL 
sop_adherence   double NULL 
end_date    datetime(6) NULL    
batch_median_health int(10) unsigned NULL   
batch_name  varchar(200) NULL   
commodity_name  varchar(200) NULL   
pending_tasks   int(10) unsigned NULL   
pest_attack varchar(500) NULL   

when I trying to get the object from this table like this

Batch.objects.filter(farm_id = farm_id, batch_status='completed')

farm_id variable is already defined
then I should all the data column for that Batch
But I am not getting ALL all columns I am getting an only batch name as an output

 (2706) Papaya | 2021-03-18
 (2707) Papaya | 2021-03-18

How can I get all the columns in dict ??
and so that I can use this in the filter of batchyield

batch_id Is a foreign key in batchyield which is batch table ID

and get batchyield all data
by

Batchyield.objects.filter(batch_id=batch_id)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2025-01-18 10:29:20

试试这个:

batch_ids = Batch.objects.filter(
    farm_id = farm_id, batch_status='completed'
).values_list('id', flat=True)

batch_yields = Batchyield.objects.filter(batch_id__in=batch_ids)

values_list('id', flat=True) 返回 Batch 对象的 id 列的查询集

Try this:

batch_ids = Batch.objects.filter(
    farm_id = farm_id, batch_status='completed'
).values_list('id', flat=True)

batch_yields = Batchyield.objects.filter(batch_id__in=batch_ids)

values_list('id', flat=True) returns a queryset of id columns of the Batch objects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文