Hi group members,
I’m working on an app where I have products model, and I want to give the admin the possibility of ordering products displayed in product change list admin. when the admin make a filter and get all products I want to be able to reorder interactively this list.
this is my product model:
class Product(models.Model):
name = models.CharField(max_length=500)
is_active = models.BooleanField(default=True)
category = models.ForeignKey(
"categories.Category", on_delete=models.CASCADE, related_name="products"
)
description = models.TextField(null=True, blank=True)
ingredient = models.TextField(null=True, blank=True)
order = models.PositiveIntegerField(default=0)
price = models.DecimalField(max_digits=10, decimal_places=2, default=0)
discount_percent = models.DecimalField(max_digits=10, decimal_places=2, default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = ProductManager()
class Meta:
verbose_name_plural = _("Products")
verbose_name = _("Product")
ordering = ["order"]
def __str__(self) -> typing.Text:
return self.name
@property
def discounted_price(self) -> Decimal:
if self.discount_percent:
return self.price - (self.price * (self.discount_percent / 100))
return self.price
@property
def parent_category(self):
if not self.category.parent:
return "-"
return self.category.parent
I tried to use the field order to reorder the list but I can’t get it in the “list_editable” even if it is in the “list_display” I got the error :
(admin.E122) The value of ‘list_editable[0]’ refers to ‘order’, which is not contained in ‘list_display’.
I registered the model as below :
class ProductAdmin(SortableAdminMixin, TranslationAdmin):
inlines = [ImagesInline, NutritionsInline]
search_fields = ["name", "description"]
autocomplete_fields = ["category"]
fieldsets = (
(
_("Details"),
{
"fields": (
"is_active",
"name",
"price",
"discount_percent",
"parent_category",
"category",
),
},
),
(
_("Product Info"),
{
"fields": ("description",),
},
),
)
readonly_fields = ["parent_category"]
list_editable = ["order",]
list_display = ("order", "name", "parent_category", "category")
list_filter = [CategoryFilter]
Is it possible to make “order” field editable in admin change list so when the filter of category is applied we can reorder the product that belong to the specific category that we get by editing the “order” field?