0

I have the following model

class Products(models.Model):

  name = models.CharField()
  description = models.CharField()


  permissions = models.ManyToManyField(CustomPermissions)

class CustomPermissions(models.Model):
  code_name = models.CharField()
  description = models.CharField()
  price = models.CharField()

admin.py

class ProductAdmin(admin.ModelAdmin):
   
  list_display = ["can_edit_product", "can_edit_price"]

Right now these fields are displayed as boolean fields instead I want to show them as checkboxes

Viktor
  • 722
  • 1
  • 8
  • 25
  • Does this answer help any? https://stackoverflow.com/questions/4784567/show-a-manytomanyfield-as-checkboxes-in-django-admin – SamSparx Jul 19 '23 at 11:45
  • I've tried this but this makes checkboxes appear on the changes page instead of the view page – Viktor Jul 19 '23 at 13:06

1 Answers1

0

If you are already getting a result for "can_edit_product" I assume it's already a method for the model or handled elsewhere. If so, I believe you can do this by adding a method to your admin class, instructing it to display it using the admin images, as follows:

class ProductAdmin(admin.ModelAdmin):

   list_display = ["can_edit_product_permission", "can_edit_price_permission"]
   
   #decorator to use admin yes/unknown/no images
   @admin.display(
        boolean=True
        description = "Can edit product?")
   def can_edit_product_permission(self, obj):
        return obj.can_edit_product

   
SamSparx
  • 4,629
  • 1
  • 4
  • 16
  • Not exactly what i need, i need checkboxes to be able to select multiple items and change permissions at once – Viktor Jul 20 '23 at 05:19
  • So you want to change an admin view page into an edit form...that isn't the object's edit form? You might be better off just making your own page with formsets and requiring admin permissions rather than changing the basic purpose of the admin page types. – SamSparx Jul 20 '23 at 10:34