Mažas
Mažas

Reputation: 397

django admin custom order for foreign key objects

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.

Upvotes: 0

Views: 317

Answers (1)

ger.s.brett
ger.s.brett

Reputation: 3308

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

class VariantInline(admin.StackedInline):
    model = Variant
    ordering = ("stuff",)

Upvotes: 2

Related Questions