Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

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 students, but not the teachers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...