1👍
✅
Well since you have a list with names, I think this could help you:
names_list = [ ... ]
A.objects.filter(b__name__in=names_list, c_id=1, status=1).values('b__id', 'text')
Where names_list
is your initial list of B names.
EDIT:
From your edit I can see the problem you’re facing. I think you will get an error in this line:
s = A(status=2, b_id=x.b_id, text=x.text, c_id=1)
because in x = A.objects.filter(b__name=n, c_id=1, status=1).values('b__id', 'text')
you will get a QuerySet
not an A
object since you are using .filter()
and not .get()
method.
If you need to save a new entry with a updated status, for every A
object that match your first query, you could try this:
for n in name_list:
x_objs = A.objects.filter(b__name=n, c_id=1, status=1)
for x in x_objs:
s = A(status=2, b=x.b, text=x.text, c_id=1)
s.save()
Source:stackexchange.com