Created
July 31, 2008 07:34
-
-
Save qingfeng/3419 to your computer and use it in GitHub Desktop.
Django admin m2m management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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