I create a definition to get the percentage of completed tasks associated with a project.
JavaScript
x
7
1
class ProjectAdmin(ImportExportModelAdmin,admin.ModelAdmin):
2
#...
3
def percentage(sef, obj):
4
return obj.todo_set.filter(state = True).count() * 100 / obj.todo_set.all().count()
5
#...
6
list_display = ['project_title', 'category_list', 'date_format', 'crq_count', 'task_count', 'percentage', 'file_count', 'state']
7
I want to eliminate the decimals and transform it into whole numbers
Finally, the idea is that if all the tasks are completed, the project status should go to finished
JavaScript
1
4
1
class Project(models.Model):
2
#...
3
state = models.BooleanField(blank=True, null = True, default = False)
4
Advertisement
Answer
You can round the value you obtain when retrieving the percentage:
JavaScript
1
8
1
class ProjectAdmin(ImportExportModelAdmin,admin.ModelAdmin):
2
3
# …
4
5
def percentage(sef, obj):
6
return round(
7
obj.todo_set.filter(state=True).count() * 100 / obj.todo_set.all().count()
8
)