在django管理界面中包含来自另一个模型的多对多

时间:2021-11-08 11:28:01

Greetings, I'm sure there is a simple solution to what I'm trying to do but unfortunately I wasn't able to find it in the documentation.

你好,我确信有一个简单的解决方案可以解决我的问题,但不幸的是,我在文档中找不到它。

I have the following model (simplified version shown):

我有以下模型(简体版本):

models.py:

models.py:

class Student(models.Model):
  student_id = models.IntegerField(primary_key=True, unique=True,
      db_index=True, max_length=9)
  first_name = models.CharField(max_length=50)
  last_name  = models.CharField(max_length=50)

  def __unicode__(self):
    return u"%s %s" % (self.first_name, self.last_name)

class Course(models.Model):
  course_id = models.AutoField(primary_key=True, unique=True,
                              db_index=True, max_length=4)
  title = models.CharField(max_length=50)
  dept = models.CharField(max_length=6)
  number = models.IntegerField(max_length=5)
  student_id = models.ManyToManyField(Student, blank=True)

  def __unicode__(self):
    return u"%s %s" % (self.dept, self.number)

What I wanted was to be able to add students to multiple classes in the admin interface similar to the way that I can add students in the classes admin interface. If there is yet another way that seems more beneficial I would be interested in that as well.

我想要的是能够将学生添加到管理界面中的多个类中,就像我可以在类管理界面中添加学生一样。如果还有其他更有益的方法,我也会感兴趣。

Thanks in advance for any help!

谢谢你的帮助!

1 个解决方案

#1


1  

You can use the inlines in your related model, or this blog post might be of some help.

您可以在相关的模型中使用inline,或者这篇博文可能会有所帮助。

#1


1  

You can use the inlines in your related model, or this blog post might be of some help.

您可以在相关的模型中使用inline,或者这篇博文可能会有所帮助。