List Info

Thread: Choices from table vs. Foreign Key relationship.




Choices from table vs. Foreign Key relationship.
user name
2007-12-17 18:05:02
So the recommendation seems to be when your selection
choices are not
static you should go ahead and use a Foreign Key
relationship. You get
the automatic loading of the table into a select widget
etc.

But there's this issue of the cascade on delete.

I have a Student model with a Foreign Key relationship to
Teacher.
When a teacher is removed, so go the students. Too dangerous
for my
particular scenario. There doesn't seem to be a Django
feature that
allows for adjusting the cascade.

I thought to uncouple the relationship and use the Teacher
model
strictly as a 'lookup table.'

What are best practices on this? What will I be losing if I
just load
the teachers as 'choices' on the student forms? Is it worth
creating
my own cascade protection to keep the Foreign Key
relationship?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Choices from table vs. Foreign Key relationship.
country flaguser name
Denmark
2007-12-18 03:53:59
radioflyer wrote:
> So the recommendation seems to be when your selection
choices are not
> static you should go ahead and use a Foreign Key
relationship. You get
> the automatic loading of the table into a select widget
etc.
> 
> But there's this issue of the cascade on delete.
> 
> I have a Student model with a Foreign Key relationship
to Teacher.
> When a teacher is removed, so go the students. Too
dangerous for my
> particular scenario. There doesn't seem to be a Django
feature that
> allows for adjusting the cascade.
> 
> I thought to uncouple the relationship and use the
Teacher model
> strictly as a 'lookup table.'
> 
> What are best practices on this? What will I be losing
if I just load
> the teachers as 'choices' on the student forms? Is it
worth creating
> my own cascade protection to keep the Foreign Key
relationship?

Don't delete the Teacher.

Instead, I would add a boolean field 'active' to the model
and change 
the value on delete.

-- 
Christian Joergensen | Linux, programming or web
consultancy
http://www.razor.dk  |    
Visit us at: http://www.gmta.info

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Choices from table vs. Foreign Key relationship.
user name
2007-12-18 09:15:47
On Dec 17, 2007 7:05 PM, radioflyer <solidethergmail.com> wrote:
> I have a Student model with a Foreign Key relationship
to Teacher.
> When a teacher is removed, so go the students. Too
dangerous for my
> particular scenario. There doesn't seem to be a Django
feature that
> allows for adjusting the cascade.

Personally, I'd keep the ForeignKey relationship, but just
override
Teacher.delete() so that removes itself from all of its
Students
before being deleted. Something like this should do it:

class Teacher(models.Model):
    # ... Fields here
    def delete(self):
        for student in self.student_set.all():
            student.teacher = None
            student.save()
        super(Teacher, self).delete()

This, of course, assumes that you created your students
include a
field looking something like this:

teacher = ForeignKey(Teacher, null=True)

Would that work for you?

-Gul

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Choices from table vs. Foreign Key relationship.
user name
2007-12-18 21:44:41
Seems like you'd want a many-to-many relationship, no?

Each student gets multiple teachers, and each teacher has
multiple
students, unless all my years in school were for nothing
.

Or am I missing something?

Does delete remove FK's with M2M?

J

On Dec 17, 4:05 pm, radioflyer <solidet...gmail.com> wrote:
> So the recommendation seems to be when your selection
choices are not
> static you should go ahead and use a Foreign Key
relationship. You get
> the automatic loading of the table into a select widget
etc.
>
> But there's this issue of the cascade on delete.
>
> I have a Student model with a Foreign Key relationship
to Teacher.
> When a teacher is removed, so go the students. Too
dangerous for my
> particular scenario. There doesn't seem to be a Django
feature that
> allows for adjusting the cascade.
>
> I thought to uncouple the relationship and use the
Teacher model
> strictly as a 'lookup table.'
>
> What are best practices on this? What will I be losing
if I just load
> the teachers as 'choices' on the student forms? Is it
worth creating
> my own cascade protection to keep the Foreign Key
relationship?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )