Multiple models and a primary key

24 views
Skip to first unread message

dahlen...@gmail.com

unread,
Feb 16, 2018, 4:08:55 PM2/16/18
to Django users
Ok, thanks for replying. am quite new in Django. Am looking for a way to use a primary key in 3 models in my django project. for instance. The models are Profile, Subject and Grade. Want subject to link to profile and grade to link to subject.

class Profile(models.Model): f_name = models.CharField(max_length=20) l_name = models.CharField(max_length=20) sch_id = models.CharField(max_length=20) class Subject(models.Model): profile = models.ForeignKey(Profile, on_delete=models.CASCADE) maths = models.CharField(max_length=20) english = models.CharField(max_length=20) class Grade(models.Model): subjt = models.ForeignKey(Subject, on_delete=models.CASCADE) score = models.CharField(max_length=20) grad = models.CharField(max_length=20)

I don't know to archive this.

Michael MacIntosh

unread,
Feb 16, 2018, 8:43:08 PM2/16/18
to django...@googlegroups.com
I'm not sure what you are trying to achieve, but you probably don't want all of the models to use the same primary key.
You can access the primary key though via the pk or id attributes.  For instance:
Grade.objects.all().values("subjt_id", "subjt__pk", "subjt__profile_id", "subjt__profile__pk")
However, I am not sure what you are trying to do.

Hope that helps!
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5febac08-6b1f-4c8a-a067-6abaf3f162a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
This message has been scanned for viruses and dangerous content by
E.F.A. Project, and is believed to be clean.
Click here to report this message as spam.

麦田的守望者

unread,
Feb 16, 2018, 11:18:07 PM2/16/18
to django-users
OnetoOneField may what you want!


---Original---
From: "Michael MacIntosh"<mmaci...@linear-systems.com>
Date: 2018/2/17 09:42:29
To: "django-users"<django...@googlegroups.com>;
Subject: Re: Multiple models and a primary key
Reply all
Reply to author
Forward
0 new messages