Re: Backwards in the relationship chain?
instanceOfCourseCatalog.teacher
and
instanceOfCourseCatalog.teacher.id
Bill
I have spent hours trying to find the answer to this. What makes it even more frustrating is that I'm new to django development and not a real programmer by trade (I am a UNIX admin) so I am struggling to label what I am trying to do so that I can search for an answer.
Here's what I have:
In my models.py file I have the following class definitions:
class Family(models.Model):
...
FamilyID = models.AutoField(primary_key=True)
Name = models.CharField(verbose_name="Family Name")
------------------------------------------------------------------------------------------------
class FamilyMember(AbstractUser):
....
ID = models.AutoField(primary_key=True)
FamilyID = models.ForeignKey(Family, blank=True, null=True)
...
------------------------------------------------------------------------------------------------
class Teacher(models.Model):
...
TeacherID = models.AutoField(primary_key=True)
ID = models.OneToOneField(FamilyMember)
...
------------------------------------------------------------------------------------------------
class CourseCatalog(models.Model):
...
TeacherID = models.ForeignKey(Teacher, blank=True, null=True)
...
------------------------------------------------------------------------------------------------
So, there is a table called Family that I belong to. It holds my family name and a couple of other fields.
There is then a one-to-many relationship from that to the FamilyMember table. In this table I have a record for each member of my family.
The FamilyMember table is also being used as the authentication table for my site. I have not replaced the default security model but only abstracted it and added additional fields.
I am a teacher so there is also a record in the Teacher table. This table contains the FamilyMember.ID field.
That's the setup for me. My main information is in the FamilyMember table and my FamilyMember.ID field is stored in the Family and the Teacher tables.
On to the list of classes...
There is a table called CourseCatalog that contains information about a class that I will teach. One of the columns is called TeacherID and this stores the TeacherID from the Teacher table. I set it up this way so that an admin can add a class and then assign it only to a teacher. The drop down list should not include FamilyMembers that are not teachers.
Here are my questions and problems:
- I want to log into the website as myself and modify only my classes. It would also be OK to modify the classes of everyone in my family. Either solution would be fine. However, I can't figure out how to do either of these things.
- It seems to me that what I'm doing is trying to go backwards in the relationship chain. I have my CourseCatalog record that includes the TeacherID. I need to look that ID up in the Teacher table to get the FamilyMember.ID. I then need to use that to look up the record in the FamilyMember table. Once I have that, I can get the username and compare it to the username of the logged in user (me). Finally, I can filter on records where these two match.
- I'm wondering if I have architected my database wrong. Is there an easier way to do this? Maybe I should get rid of the Teacher table and just use a boolean for teacher in the FamilyMember table. The concern that I have with this is when I start working on the Student table. I need to store more information about a student (allergy, emergency contacts, etc) and don't think it is appropriate to add those fields to the FamilyMember table because they are only used for students.
- I'm asking for you experts to help guide me through this. Any advice is greatly appreciated.
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home