'Updating field in admin change list

I have a model of products, and want to display the list on the admin site with the possibility of changing the order.

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)
    rank = models.PositiveIntegerField(default=0)
    price = models.DecimalField(max_digits=10, decimal_places=2, default=0)

    objects = ProductManager()

    class Meta:
        verbose_name_plural = _("Products")
        verbose_name = _("Product")
        ordering = ["order"]

    def __str__(self) -> typing.Text:
        return self.name

I decided to order the model by the field order which can't be editable in admin page, so I want to copy it's value to the field rank which I want to make editable in admin page, and when I will save the changes I made to rank I want to override the order values with the new value from rank. This is how I register the model:

class ProductAdmin(SortableAdminMixin, TranslationAdmin):
    inlines = [ImagesInline, NutritionsInline]
    search_fields = ["name", "description"]
    autocomplete_fields = ["category", "brand", "manufacturer"]
    fieldsets = (
        (
            _("Details"),
            {
                "fields": (
                    "is_active",
                    "name",
                    "price",
                    "category",
                    "description",
                ),
            },
        ),
        (
            _("Product Info"),
            {
                "fields": ("ingredient",),
            },
        ),
    )
    list_display = ("order", "rank", "name", "category")
    list_editable = ["rank"]

I tried to override the get_queryset but it didn't work. have you any idea how to do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source