django:manytomanyfield with through 如何出现在管理中? [英] django: how does manytomanyfield with through appear in admin?

查看:16
本文介绍了django:manytomanyfield with through 如何出现在管理中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所述,manytomanyfieldthrough 如何出现在管理站点中?

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)

在管理站点中,对于模型 SchoolClass,我有一个用于参加学生的字段,但没有用于教师的字段.

In the admin site, for the model SchoolClass, I have a field for attending students, but not the teachers.

推荐答案

你应该使用 InlineModelAdmin.文档.

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

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

admin.site.register(SchoolClass, SchoolClassAdmin)

这篇关于django:manytomanyfield with through 如何出现在管理中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆