开发者

django: how does manytomanyfield with through appear in admin?

As stated in the title how does manytomanyfield with through appear in the admin site?

class SchoolClass(models.Model):
    id = models.AutoField(primary_key = True)
    class_name = models.TextField()
    level = models.IntegerField()
    taught_by = models.ManyToManyField(User,related_name="teacher_teaching",through='TeachSubject')
    attended_by = models.ManyToManyField(User,related_name='student_attending')

    def __unicode__(self):
        return self.class_name
    class Meta:
        db_table = 'classes'


class TeachSubject(models.Model):
    teacher = models.ForeignKey(User)
    class_id  = models.ForeignKey(SchoolClass)
    subject = models.ForeignKey(Subject)

In the admin site, for the model SchoolClass, I have a field for attending stu开发者_开发百科dents, but not the teachers.


You should use InlineModelAdmin. Docs.

class TeachSubjectInline(admin.TabularInline):
    model = TeachSubject
    extra = 2 # how many rows to show

class SchoolClassAdmin(admin.ModelAdmin):
    inlines = (TeachSubjectInline,)

admin.site.register(SchoolClass, SchoolClassAdmin)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜