Adarsh Srivastav
Adarsh Srivastav

Reputation: 160

djagngo get all .objects.filter() with condtion

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)

Upvotes: 0

Views: 50

Answers (1)

Ersain
Ersain

Reputation: 1520

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

Upvotes: 1

Related Questions