Skip to content

Instantly share code, notes, and snippets.

@funkybob
Created May 23, 2012 03:51
Show Gist options
  • Save funkybob/2773175 to your computer and use it in GitHub Desktop.
Save funkybob/2773175 to your computer and use it in GitHub Desktop.
from django.contrib import admin
from django import forms
from .models import *
class ThroughInline(admin.TabularInline):
model = Through
raw_id_fields = ('unter',)
sortable_field_name = 'order'
formfield_overrides = {
models.PositiveIntegerField: { 'widget': forms.HiddenInput }
}
autocomplete_lookup_fields = {
'pk': ('unter',),
}
class UberAdmin(admin.ModelAdmin):
inlines = [
ThroughInline,
]
admin.site.register(Uber, UberAdmin)
from django.db import models
class Uber(models.Model):
title = models.CharField(max_length=1000)
unters = models.ManyToManyField('Unter', through='Through', blank=True)
class Unter(models.Model):
name = models.CharField(max_length=1000)
@staticmethod
def autocomplete_search_fields():
return ('name__icontains',)
class Through(models.Model):
uber = models.ForeignKey('Uber')
unter = models.ForeignKey('Unter')
order = models.PositiveIntegerField()
class Meta:
unique_together = (
('uber', 'unter'),
)
ordering = ('order',)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment