[Django]-Django ordered ManyToManyField in admin interface

5👍

This is quick and a bit rough, but it should get you close(r).

from django.db import models
from django.contrib import admin

class Document(models.Model):
    name = models.CharField(max_length = 128)

    def __unicode__(self):
        return self.name

class Author(models.Model):
    name = models.CharField(max_length = 128)
    document = models.ManyToManyField(Document, through = 'Foo')

    def __unicode__(self):
        return self.name

class Foo(models.Model):
    document = models.ForeignKey(Document)
    author = models.ForeignKey(Author)
    author_order = models.IntegerField()

class FooInline(admin.TabularInline):
    model = Foo

class DocumentAdmin(admin.ModelAdmin):
    inlines = [ FooInline ]

admin.site.register(Author)
admin.site.register(Document, DocumentAdmin)

http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects

0👍

this might help you too – there is some utility in Django to build models for the legacy databases.

I’ve built a model for mediawiki database using it. It gets most of the things right, but you’ll need to tweak the models a little.

👤Evgeny

Leave a comment