Mohammad6pari
Mohammad6pari

Reputation: 79

change value of boolean field in admin pannel in Django

The codes of my models.py and admin.py are as follows: models.py:

from django.db import models
class Product(models.Model):
    name = models.CharField(max_length=50)
    price = models.IntegerField()
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    company = models.ForeignKey(Company, on_delete=models.CASCADE)
    is_active = models.BooleanField(default=True)

    def __str__(self):
        return '{}, {}'.format(self.name, self.company)

admin.py :

from django.contrib import admin

from .models import Product


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'category', 'is_active']
   # list_filter=['is_active']
    list_display_links = ['id', 'name']
    fieldsets = (
        ('Identification', {
            'fields': ('name', 'price','is_active')
        }),
        ('Details', {
            'classes': ('collapse',),
            'fields': ('category', 'company')
        }),
    )
    actions=['make_inactive']
    def make_inactive(self, request, queryset):
        queryset.update(is_active=False)
    make_inactive.allow
        

And the output of this code in the admin model is as follows

pric1

how should i change my code to have a ouput like the following picture (difference is in checkBox of is_active column): pic2

Upvotes: 1

Views: 821

Answers (1)

Mohammad6pari
Mohammad6pari

Reputation: 79

we can make is_active column editable (checkBox option) by adding this code

list_editable=['is_active']

to class ProductAdmin

Upvotes: 4

Related Questions