Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 861
goh Avatar asked May 10 '11 09:05

goh


People also ask

What is the Changelist in Django admin?

Django Admin's "change list" is the page that lists all objects of a given model. Now, all your articles should have a different name, and more explicit than "Article object".

How does Django admin work?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.


1 Answers

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) 
like image 106
DrTyrsa Avatar answered Sep 30 '22 08:09

DrTyrsa