0

I want to link the Subject model with Class in such a way that every student belonging of Class model will have same subjects included by user in Subject models linked via connect_class.

Inside models.py of school app.

from django.db import models
from accounts.models import School


class Class(models.Model):
    connect_school = models.ForeignKey(School, on_delete=models.SET_NULL, null=True)
    class_list = models.CharField(max_length=95)
    def __str__(self):
            return self.class_list

class Subject(models.Model):
    connect_class = models.ForeignKey(Class, on_delete=models.SET_NULL, null=True)
    subjects = models.CharField(max_length=95, null=True)
    def __str__(self):
        return self.subjects

Inside models.py of student app:

from django.db import models
from django.utils import timezone
import nepali_datetime
from school.models import Class, Subject
from accounts.models import School
connect_school = models.ForeignKey(School, on_delete=models.CASCADE, null=True)
...name,gender, etc. ...
Class = models.ForeignKey(Class, on_delete=models.CASCADE, null=True)
subject = models.ManyToManyField(Subject)

Inside models.py of accounts.

from django.db import models
from django.utils import timezone
import nepali_datetime

# from django.contrib.auth.models import User
# Create your models here.

class School(models.Model):
    school_name = models.CharField(max_length=50)
    ...
    username = models.CharField(max_length=100)
    password = models.CharField(max_length=95)
    ...
    principal = models.CharField(max_length=150, default='')
    ....
  • 1
    The renaming ideas for Django are give here: [Django Renaming IDEAS](https://stackoverflow.com/questions/25091130/django-migration-strategy-for-renaming-a-model-and-relationship-fields) –  Nov 12 '20 at 11:38

1 Answers1

0

Step 1:

First, migrate the base models so that they don't make any conflicts. For you care you need to migrate School models because this model is linked with Foreign Key with various other model.

python manage.py migrate school

school is the app_name of your Djang app.

Step 2:

After that you need to migrate other branches of your model.