After a preliminary reading of your answer, the second solution seems like what I am trying to do. Basically this is a simple Track and Field competition manager, and one year a student will compete as a midget and get a result for the 100m dash, and the next year compete as a junior and get a result for the 100m dash.
Thank you for your time and effort. On Jan 22, 5:15 pm, Russell Keith-Magee <freakboy3...@gmail.com> wrote: > On Thu, Jan 22, 2009 at 10:57 PM, jason.t.stein <jason.t.st...@gmail.com> > wrote: > > > Perhaps this is an sql question, and not a Django question so please > > have patience. I am writing a student management app that keeps track > > of student events. The relevant tables are: > > > class Student(models.Model): > > first_name = models.CharField(max_length=32) > > last_name = models.CharField(max_length=32) > > birth_date = models.DateField('Student Birth Date') > > gender = models.CharField(max_length=1, choices = GENDER_CHOICES) > > school = models.ForeignKey(School) > > > class Competitor(models.Model): > > student = models.ForeignKey(Student) > > track_meet = models.ForeignKey(TrackMeet) > > age_category = models.CharField(max_length=1, choices = > > AGE_CATEGORY_CHOICES) > > event = models.CharField(max_length=16, choices = EVENT_CHOICES) > > > I want to set age_category, based on the age of a student on a certain > > date. I can calculate the appropriate age, but am interested how to > > set the age_category based on this calculation. > > > Any help is greatly appreciated. Thank you for your time. > > Hi Jason, > > There are several possible answers to your question, depending on how > and when you want to set the age_category. > > Are you looking to do this as a bulk SQL operation (i.e., update every > object at once)? In which case, the best way will be in raw SQL - you > will need to investigate the CASE operator on your database of choice. > > Alternatively, if you are looking to set the initial age category when > the competitor record is saved to the database, then all you need to > do is override the save() method on your competitor. The following > code is untested (and not particularly accurate as an age > calculation), but should give you an idea of the approach: > > def save(self, *args, **kwargs): > age = (date.today() - self.student.birth_date) / 365 > if age > 10: > self.age_category = AGE_CATEGORY.SENIOR > else: > self.age_category = AGE_CATEGORY.JUNIOR > super(Competitor, self).save(*args, **kwargs) > > It's also worth asking whether the age category needs to be in the > database at all. If it is purely derived from the age of the student, > there may be an argument for not storing it in the database at all, > and computing the age category whenever it is required. > > Hope this helps, > > Yours, > Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---