Skip to content

Instantly share code, notes, and snippets.

@qingfeng
Created July 31, 2008 07:34
Show Gist options
  • Save qingfeng/3419 to your computer and use it in GitHub Desktop.
Save qingfeng/3419 to your computer and use it in GitHub Desktop.
Django admin m2m management
from django.contrib import admin
from app1.models import *
class MembershipInline(admin.TabularInline):
model = Membership
extra = 1
class PersonAdmin(admin.ModelAdmin):
inlines = (MembershipInline,)
class GroupAdmin(admin.ModelAdmin):
inlines = (MembershipInline,)
admin.site.register(Person, PersonAdmin)
admin.site.register(Group, GroupAdmin)
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
import unittest
from datetime import datetime
from django.test.client import Client
from app1.models import *
class ModelsTestCase(unittest.TestCase):
def setUp(self):
self.p1 = Person.objects.create(name="qingfeng")
self.g1 = Group.objects.create(name="python")
self.m1 = Membership.objects.create(person=self.p1,group=self.g1,date_joined=datetime.now())
self.c = Client()
def testStr(self):
self.assertEquals(str(self.p1),"qingfeng")
self.assertEquals(str(self.g1),"python")
def testUrl(self):
response = self.c.get("/hello/")
self.assertEquals(response.content,"Hello Jython And Django!")
self.assertEquals(response.status_code,200)
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^mytrunk/', include('mytrunk.foo.urls')),
# Uncomment this for admin:
('^admin/(.*)', admin.site.root),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment