0

I have models:

class Product(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name

class Variant(models.Model):
    product = models.ForeignKey('Product', related_name='variants', null=True)
    stuff = models.CharField(max_length=255)

admin.py:

class VariantInline(admin.StackedInline):
    model = Variant

class ProductAdmin(admin.ModelAdmin):
    inlines = [VariantInline]
    list_display = ['name']

Is it possible to change order of variants in product's admin? I searched in google but found apps only for ordering whole model, not inlines. Order items with the django-admin interface

EDITED: I need possibility to insert variants. For example, lets say in Product i have 10 variants. Then i have to insert one in middle of them. This is what i need.

Community
  • 1
  • 1
Mažas
  • 387
  • 1
  • 6
  • 19

1 Answers1

2

According to the docs you should be able to use ordering:

class VariantInline(admin.StackedInline):
    model = Variant
    ordering = ("stuff",)
ger.s.brett
  • 3,267
  • 2
  • 24
  • 30
  • I think you misunderstood. I need custom ordering, which means not by some key, like "stuff". Lets say i have 10 variants and i want insert one in the middle of them. This is what i need. – Mažas Apr 27 '16 at 18:03
  • Could you be more specific please? There needs to be a field or some piece of logic that contains the ordering information. – ger.s.brett Apr 27 '16 at 18:36