开发者

django: how does manytomanyfield with through appear in admin?

开发者 https://www.devze.com 2023-03-04 16:11 出处:网络
As stated in the title how does manytomanyfield with through appear in the admin site? class SchoolClass(models.Model):

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

精彩评论

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