S
S
Sergey Eremin2015-01-20 22:34:24
Django
Sergey Eremin, 2015-01-20 22:34:24

How to display many-to-many relationships in both tables in Django admin?

Let's say we have a school, and one lesson can be taught by several teachers, and teachers are generalists and can lead various lessons.
models.py:

# Таблица для хранения УЧИТЕЛЕЙ
class Coach (models.Model):
    CoachHeader = models.CharField(
        max_length = 256,
        help_text = u"Имя Фамилия"
    )
    CoachPhoto =  models.ImageField(
        max_length = 255,
        upload_to = "coach_img/",
        default = "",
        null = True,
        blank = True,
        help_text = u"Путь до картинки",
    )

    def __unicode__(self):
        return u"%03d: %s" % (self.id, self.CoachHeader)

# Таблица для хранения ЗАНЯТИЙ
class Lesson (models.Model):
    LessonHeader = models.CharField(
        max_length = 256,
        help_text = u"Название занятия"
    )
    LessonAd = models.CharField(
        max_length = 1024,
        unique = False,
        null = True,
        blank = True,
        help_text = u"Анонс (краткое описание)"
    )
    Lesson2Coach = models.ManyToManyField(
        "Coach",
        null = True,
        blank = True,
        help_text = u"Учителя ведущие занятие"
    )

    def __unicode__(self):
        return u"%03d: %s" % (self.id, self.LessonHeader)

In the admin panel, you can set the link LESSON→TEACHER only when creating/editing records in the LESSON table. This is not convenient, because communication is two-way. Is there any way to tell models.py or admin.py to show the link field when working with the Coach table and the Lesson table?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2015-01-21
@xmdy

You need to use inlines
https://docs.djangoproject.com/en/1.7/ref/contrib/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question