Django admin inline-model customization

Go To StackoverFlow.com

1

Is there a way to de-couple django admin inline-models from clustering like models together?

A bit of context: I have a model named Page with two inline-models, TextBlock and GalleryContainer. I would to render TextBlocks and GalleryContainers on a template based on the order they're added in the Page admin editor. The default django-admin display looks like this:

django-admin display

I would like it to display as:

  • Gallery Container 1
  • Textblock 1
  • Gallery Container 2

But I have no idea how to do that. Any suggestions or nudges in the right direction would be a great help. Thanks in advance. (I also hope my question makes sense...)

2012-04-05 18:57
by ploosh
I think it's easier to move these blocks with JS after the page is loade - ilvar 2012-04-06 05:53


0

If you want a relation between a column and a gallery your models should reflect that. So if I understand correctly: A page has name and columns. A column has text, gallery (optional) and ordering.

models.py:

class Page(models.Model):
   name = models.CharField(max_length=200)

class Gallery(models.Model):
   name = models.CharField(max_length=200)

class Column(models.Model):
   page = models.ForeignKey(Page)
   text = models.TextField()
   gallery = models.ForeignKey(Gallery, null=True, blank=True) # Optional
   ordering = models.IntegerField()

   class Meta:
       ordering = ('ordering', )

This example shows how ordering is done by an IntegerField. If you want to order the columns based on the moment they where added replace

ordering = models.IntegerField()

with

models.datetimeField(auto_now_add=True)

In your admin.py:

class ColumnInline(admin.TabularInline): # or StackedInline
    model = Column

class PageAdmin(admin.ModelAdmin):
    inlines = [
        ColumnInline,
    ]

Note: I put your 'gallery display name' in Gallery as 'name'. Makes more sense to give the gallery it's name only once. But if a gallery is in more places with different names, than you need a field (e.g. 'gallery_display_name=models.CharField(max_lenght=200)') on the Column model.

Is this the answer to your question? I hope it helps!

2012-04-06 17:11
by allcaps
Ads