Reputation: 969
I have a dataframe of related values that I need to query my object together. I can loop through this list of related values and give them to my object, and then append them to a list, but this gives me a list of separate querysets. Is there a way I can read in my tuples together without a loop and obtain a single queryset?
The related values are hts_run and variant. In this case, the same variant value has multiple hts_run values. my dataframe has removed the unwanted hts_run values, so I have the correct variant corresponding to the correct hts_run value.
here is my code at the moment:
var_obj = []
for i, row in df.iterrows():
v_obj = row['variant']
var_obj.append(VariantSampleRun.objects.filter(sample=current_sample,
hts_run=row['run']).select_related('variant').order_by('variant'))
I need however a single var_obj queryset for another function. I have tried to use 'chain' to merge them, but I am thinking
Upvotes: 0
Views: 258
Reputation: 1
instead of df.iterrows()
you just execute
runs=model.objects.filter().values_list('run', flat=True)
then pass tuple to next filter like
filter(hts_run__in=runs)
Upvotes: 0
Reputation:
If i'm understand you may use __in
filter, like this:
runs = [row['run'] for i, row in df.iterrows()]
var_obj = VariantSampleRun.objects.filter(sample=current_sample,
hts_run__in=runs).select_related('variant').order_by('variant')
Upvotes: 1