I am trying write a method for one of my models, that will end up with a list of boolean values based on a manytomany field. So I am looping though category id's and comparing them with a queryset, but I can't work out how to translate what I get in the shell to a function. I've been struggle with OOP for about 3 years now, which is why I started learning django in the first place.
but basically, in the django shell, I can do this to get the queryset I want:
p1 = Project.objects.get(id=2)
p1.update_categories.all().values_list('id', flat=True)
<QuerySet [1, 4]>
The problem I have is that this is for one project and I can't figure out how you translate that into something that will work for all instances. So far I have this:
def checkmark(self):
'''
Generates a list of True or False based on many to many field
for Project and UpdateCategory
'''
checkmark = []
# This is the line giving me issues
queryset = Project.objects.filter(project=self, update_categories)
# this bit all works fine
categories = UpdateCategory.objects.all().values_list('id', flat=True)
for cat_id in categories:
if cat_id in queryset:
checkmark.append(True)
else:
checkmark.append(False)
return checkmark
So what I am doing here is creating an empty list, "checkmark" (for want of a more descriptive variable name), then I am trying to recreate that same queryset in a variable "queryset", then I iterate over the different categories of which there are exactly 7 and append True if the category id is in the queryset and False if it's not. The result I am expecting is:
[True, False, False, True, False, False, False]
I've no idea if this is the right way to go about this, but if it works, it's a start.
checkmark()a method of theProjectmodel?return self.update_categories.values_list('id', flat=True)?