Drennos
Drennos

Reputation: 313

Format decimal on definition in Admin Panel Django

I create a definition to get the percentage of completed tasks associated with a project.

class ProjectAdmin(ImportExportModelAdmin,admin.ModelAdmin):
    #...
    def percentage(sef, obj):
        return obj.todo_set.filter(state = True).count() * 100 / obj.todo_set.all().count()
    #...
    list_display = ['project_title', 'category_list', 'date_format', 'crq_count', 'task_count', 'percentage', 'file_count', 'state']

enter image description here

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

class Project(models.Model):
    #...
    state = models.BooleanField(blank=True, null = True, default = False)

Upvotes: 1

Views: 369

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476624

You can round the value you obtain when retrieving the percentage:

class ProjectAdmin(ImportExportModelAdmin,admin.ModelAdmin):
    
    # …
    
    def percentage(sef, obj):
        return round(
           obj.todo_set.filter(state=True).count() * 100 / obj.todo_set.all().count()
        )

Upvotes: 1

Related Questions