在django 的admin中,若有两张表为many-to-many的关系,则使用django model类里面的ManyToManyField了。并且可以选择自定义中间表,在ManyToManyField里面添加属性through=“中间表的model类”。嘿嘿,脱离话题了,下面进入主题吧。
models.py
class Person(models.Model):
name = models.CharField(primary_key=True, max_length=128)
desc = models.CharField(max_length=256)
def __unicode__(self):
return self.name
class Meta:
db_table = u'person'
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, db_table='person_group')
def __unicode__(self):
return self.name
class Meta:
db_table = u'group'
admin.py
class GroupForm(forms.ModelForm):
members = forms.ModelMultipleChoiceField(widget=CheckboxSe