Sunday, December 6, 2009

Re: New to django, python, etc. - ForeignKey question

Thanks so much. I figured that the problem stemmed from working
within the class rather than an instance, as you suggest. The
suggestion to override the save fxn makes so much sense that I am now
embarrassed for asking the question in the first place.

It's amazing how much sense things make when someone else solves your
problem (*smacking forehead with hand*).

Very appreciated, thanks again.
Guy

On Dec 5, 9:56 pm, Sam Lai <samuel....@gmail.com> wrote:
> The error is occurring here as you have mentioned -
>
> rolestartdate = models.DateField(default = \
>                                Project.objects.get
> (pk=project.id).startdate)
>
> You're right that Django creates an attribute named id for every model
> without a primary key explicitly specified, but I suspect the issue
> here is that Django is trying to resolve 'project.id' before the
> Project model has been fully initialized. Also, Django is trying to
> resolve project.id for the model class, and not a model instance which
> won't work as model classes do not contain data.
>
> I believe 'default' can only be used to specify static values, not
> values that depend on other models (there is no defined behaviour for
> that, so if it works, it's by chance and might not work later).
>
> To do what you want, I suggest you override the save method in the
> model, and add the startdate from the related Project model to the
> Role model.
>
> I'm a bit rusty on this, but something like this should work:
>
> def save(self):
>     if self.project != None:
>         self.rolestartdate = project.startdate
>
>     #don't worry about handling if self.project == None; foreign keys
> are required by default and the superclass' save method will raise an
> exception
>     super(Role, self).save()
>
> 2009/12/6 Guy <guyea...@gmail.com>:
>
>
>
> > I am using the code shown below.  The part causing the problem is the
> > Role class which describes the role a person might have on a project.
> > As part of that role, I would have an attribute that records the start
> > and end date of the person's involvement.  The problem seems to be
> > coming when I make an ill-fated attempt to define a default start date
> > as the startdate of the project to which the role is linked.
>
> > ---------------------code------------------------------------------
> > from django.db import models
>
> > class Project(models.Model):
> >    """class to describe the attributes that characterize a project"""
>
> >    #reference number for project
> >    referencenum = models.CharField("reference number for project",
> >                                                      max_length=20)
> >    #begin date of project
> >    startdate =  models.DateField()
>
> > class Role(models.Model):
> >    """class to describe a Person object's affiliation to a Project
> > object"""
>
> >    #project in which person is involved
> >    project =   models.ForeignKey(Project)
>
> >    #begin date of person's involvement on project; this is the
> > problem line
> >    rolestartdate = models.DateField(default = \
> >                                Project.objects.get
> > (pk=project.id).startdate)
>
> > -----------------------------------------------------------------
> > when I run:
> >      manage.py syncdb
> > I get the following error:
>
> >   rolestartdate = models.DateField(default = Project.objects.get
> > (pk=project.id).st
> >   artdate)
> >   AttributeError: 'ForeignKey' object has no attribute 'id'
>
> > I was under the impression that django creates an attribute on every
> > model, foo.id, that serves as a primary key.
>
> > Many thanks in advance to anyone who can explain what what I not
> > grasping here.
>
> > --
>
> > 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+unsubscribe@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

--

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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate