1
votes

I have two apps in Django project . one is "accounts" another is " Courses "

I have a model in accounts app

class Student(SoftDeleteModel):
    user = models.OneToOneField(to=User, on_delete=models.CASCADE, related_name='student_info')
    name = models.CharField(null=True, blank=True, max_length=250)
    course_enrolled = models.ForeignKey(Courses,on_delete=models.CASCADE,blank=True)
    trx_id = models.CharField(max_length=20)
    date_of_birth = models.DateField(null=True, blank=True)
    education_qualification = models.CharField(null=True, blank=True, max_length=250)
    institution_name = models.CharField(null=True, blank=True, max_length=250)
    image = models.ImageField(upload_to='photos/%y/%m/%d',null=True, blank=True)
    address = models.CharField(null=True, blank=True, max_length=250)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.user.email

class Instractor(SoftDeleteModel):
    user = models.OneToOneField(to=User, on_delete=models.CASCADE, related_name='instractor_info')
    name = models.CharField(null=True, blank=True, max_length=250)
    degination = models.CharField(null=True, blank=True, max_length=250)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

This is model for accounts

and Course Model is:

from accounts.models import *

class Courses(models.Model):
    title = models.CharField(max_length=200,blank=True,null=True)
    image = models.ImageField(null=True,upload_to="photos/course/")
    category = models.ForeignKey(Category,on_delete=models.CASCADE)
    instractor = models.ForeignKey(Instractor, on_delete=models.CASCADE,blank=True)
    total_student_enrolled = models.IntegerField(null=True,blank=True)
    price = models.IntegerField(blank=True,null=True)
    course_length_in_hour = models.IntegerField(blank=True,null=True)
    course_length_in_weeks = models.IntegerField(blank=True,null=True)
    programming_language = models.CharField(max_length=500,null=True,blank=True)
    course_requirements = models.TextField()
    course_description = models.TextField()
    what_you_will_learn = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateField(auto_now_add=True)
    is_published = models.BooleanField(default=False)
    slug = models.SlugField(null = False, blank = True)

    def __str__(self):
        return self.title

I have import courses.model but it showing that Instractor not Defined. But if i remove

    course_enrolled = models.ForeignKey(Courses,on_delete=models.CASCADE,blank=True)

course_enrolled field from accounts model the error is not showing :/ I don't get what is the problem is .

Error Message is :

instractor = models.ForeignKey(Instractor, on_delete=models.CASCADE,blank=True) NameError: name 'Instractor' is not define

1

1 Answers

0
votes

You need to put the model name in quotes when defining a FK:

instractor = models.ForeignKey("accounts.Instractor", on_delete=models.CASCADE,blank=True)