one-to-many inline select with django admin

前端 未结 5 1661
名媛妹妹
名媛妹妹 2020-12-01 04:16

I have a standard many-to-one relationship set up. There are a bunch of fields, but for our purposes here, the relevant model is:

class Class(models.Model):
         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 04:44

    If the intention is to have students exist independently from a class, and then be able to add or remove them from a class, then this sets up a different relationship:

    • Many students can be a part of one class.
    • One student can be a part of many classes

    In reality this is describing a Many-To-Many relationship. Simply exchanging

    class Student(models.Model):
        class = models.ForeignKey(Class) ...
    

    for

    class Student(models.Model):
        class = models.ManyToManyField(Class)... 
    

    will give the desired effect in Django admin immediately

    Django Many-to-many

提交回复
热议问题