2

Here are the models:

class Teacher(models.Model):
    login = models.CharField(max_length=10, primary_key=True)
    fname = models.CharField(max_length=30)
    mname = models.CharField(max_length=30)
    lname = models.CharField(max_length=30)
class Course(models.Model):
    semester = models.ForeignKey(Semester, on_delete=models.CASCADE)
    title = models.CharField(max_length=40)
    teachers = models.ManyToManyField(Teacher.login, through='TeacherCourse')
    credits = models.IntegerField()
    numberEnrolled = models.IntegerField()
    nomenclature = models.CharField(max_length=128)
    lectures = models.IntegerField()

class TeacherCourse(models.Model):
    teacher = models.ForeignKey(Teacher.login, on_delete=models.CASCADE)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    allotedLectures = models.IntegerField()

Just added these to fresh project and tried to runserver. I'm getting the following:

AttributeError: type object 'Teacher' has no attribute 'login'

I'm missing something obvious?

2 Answers 2

1

I later found your bug from Uday Bhatye's answer:

teacher = models.ForeignKey(Teacher.login, on_delete=models.CASCADE)

is a syntax error in django.

You should do:

teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)

And to reference the login status of teachers assigned to a particular TeacherCourse:

teachercourse = TeacherCourse.objects.filter(id=teachercourse_id).select_related('teacher')
login_status = course.teacher.login

Hits the database once with select_related and fetches the login status of all teachers assigned to the TeacherCourse.

Sign up to request clarification or add additional context in comments.

2 Comments

I think, even if I run the server without migrating, it should tell me to migrate and run server successfully, provided there are no errors. Tried your suggestion. Getting the same error on makemigrations.
Yes. It is indeed a syntax error in django. Thanks for pointing out clearly. Here the login attribute is the loginid of the teacher and hence is the primary key in teacher model. I'm using already available ldap of our institute to authenticate teachers.
0

As the login field in teacher model is already declared as primary key, I need not reference it in the other two models as Teacher.login. Only referencing as Teacher suffices.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.