I don't know how to make a 1:N relationship on django's db (models.py)

Asked 2 years ago, Updated 2 years ago, 47 views

Hello, about Python, django basics on the tryhello world site

I'm going to study and make a basic lecture bulletin board.

<:/p> at models.py

class Subject(models.Model):
        subject_name = models.CharField(max_length=20)

        def __str__(self):
                return self.subject_name


class Professor(models.Model):
        professor_name = models.CharField(max_length=10)
        semester = models.Charfield(max_length=15)
        subject = models.ForeignKey(Subject)

        def __str__(self):
                return self.professor_name


class Board(models.Model):

        professor = models.ForeignKey(Professor):

        def __str__(self):
                return self.professor.professor_name

It's like this.

Here, one Professor has one Subject (1:1) form.

What I want is a form (1:N) that can have multiple subjects in one Professor.

The goal is to

What should I do?

django python3

2022-09-21 17:25

1 Answers

The current structure is not a 1:1 structure between the Professor and the Subject. Now, there is a 1:N structure with multiple Professioners per subject.

First, remove subject=models.ForeignKey(subject) from classProfessor.

And add professional = models.ForeignKey(Professional) to class Subject.


2022-09-21 17:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.