using two foreign keys in one django model

598 views
Skip to first unread message

namo

unread,
Mar 9, 2020, 6:13:40 AM3/9/20
to Django users
i'm working on a project the scenario : teacher model can upload courses , then if the teacher has not a profile the institute(model) which working at will upload the courses does this is write way i have tried in the course model?

class Course(models.Model):
    course_name = models.CharField(max_length=20)
    student = models.ManyToMany(Student)
    institute_name = models.ForeignKey(Institute,on_delete=models.CASCADE , null=True,blank=True)
    teacher_name = models.ForeignKey(Teacher ,on_delete=models.CASCADE,blank=True,null=True)
    tags = models.ManyToManyField(Category,on_delete=models.CASCADE)
    time = models.DateTimeField(auto_now_add=True)
    update = models.DateTimeField(auto_now=True)

Then define:

if user loggedin in  user.is_institute

So querying in institute model

else loggedin in user.is_teacher

And then will work on teacher model. Does this structure fine ?

I've heard that generic foreign keys not working with API

from django.contrib.contenttypes.models import ContentType


note : Teacher ,Institute have different fields name 
django:2.2

Thanks

Ryan Nowakowski

unread,
Mar 9, 2020, 10:09:24 AM3/9/20
to django...@googlegroups.com


On March 9, 2020 5:13:40 AM CDT, namo <hunarbak...@gmail.com> wrote:
>i'm working on a project the scenario : teacher model can upload
>courses ,
>then if the teacher has not a profile the institute(model) which
>working at
>will upload the courses does this is write way i have tried in the
>course
> model?

When you say profile are you taking about Django's notion of a user profile?

>
>class Course(models.Model):
> course_name = models.CharField(max_length=20)
> student = models.ManyToMany(Student)
>institute_name = models.ForeignKey(Institute,on_delete=models.CASCADE ,
>null=True,blank=True)
>teacher_name = models.ForeignKey(Teacher
>,on_delete=models.CASCADE,blank=True,null=True)
> tags = models.ManyToManyField(Category,on_delete=models.CASCADE)
> time = models.DateTimeField(auto_now_add=True)
> update = models.DateTimeField(auto_now=True)

I'd rename institute_name and teacher_name to institute and teacher. That way you can do course.teacher.name.

>
>Then define:
>
>if user loggedin in user.is_institute
>
>So querying in institute model
>
>else loggedin in user.is_teacher
>
>And then will work on teacher model. Does this structure fine ?

Please post the Institute and Teacher models.
Also are any if these models related to User?

hunar techie

unread,
Mar 10, 2020, 3:37:05 AM3/10/20
to django...@googlegroups.com

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/4U0qgySiGAY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/C87E7048-E8A5-4987-BDB6-859F8580FCCC%40fattuba.com.
Reply all
Reply to author
Forward
0 new messages