Re: NameError: name 'Job' is not defined

2009-07-10 Thread Rajesh D



On Jul 10, 8:39 am, bruno  wrote:
> Hi,
>
> I'm new in django and I have problem with importing models
>
> I have two class : Job and Event
>
> The Job class defines a method run_job() wich do something and log the
> result by creating a new Event so I need to import the Event class
> from file app/events/models.py
> The Event class reference the job who created the log entry so I need
> to import the Job class from file app/jobs/models.py
>
> file : app/jobs/models.py
> --
>
> from events.models import Event
> ...
>
> class Job(models.Model):
> ...
>     def run_job(self):
>         result = do_something()
>         e = Event(timestamp_gmt=datetime.now(), job=self,
> result=result)
>         self.save()
>         return result
>
> file : app/events/models.py
> --
>
> from jobs.models import Job
> ...
>
> class Event(models.Model):
> ...
>     timestamp_gmt = models.DateTimeField(null=False, editable=False)
>     job = models.ForeignKey(Job, blank=False, null=False)
>
> When I start the serveur I have the following error :
> ...
>   File "app\jobs\models.py", line 8, in 
>     from events.models import Event
>   File "app\events\models.py", line 12, in 
>     class Event(models.Model):
>   File "app\events\models.py", line 14, in Event
>     job = models.ForeignKey(Job, blank=False, null=False)
> NameError: name 'Job' is not defined
>
> It's like Django doesn't like the cross import between Job and Event.
> Anyone can tell me what I'm doing wrong ?
> I think the easy solution is to declar the Event class in the Job
> model file but I'd like to keep them in different files.

You could move the Event import to the run_job method which is the
only place where your code needs it:


file : app/jobs/models.py
--
class Job(models.Model):
...
def run_job(self):
from events.models import Event
result = do_something()
e = Event(timestamp_gmt=datetime.now(), job=self,
result=result)
self.save()
return result

-RD

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: NameError: name 'Job' is not defined

2009-07-10 Thread Ramiro Morales

On Fri, Jul 10, 2009 at 9:39 AM, bruno wrote:
>
> Hi,
>
> I'm new in django and I have problem with importing models
>
> I have two class : Job and Event
>
> The Job class defines a method run_job() wich do something and log the
> result by creating a new Event so I need to import the Event class
> from file app/events/models.py
> The Event class reference the job who created the log entry so I need
> to import the Job class from file app/jobs/models.py
>
>
> [...]
>
> When I start the serveur I have the following error :
> ...
>  File "app\jobs\models.py", line 8, in 
>    from events.models import Event
>  File "app\events\models.py", line 12, in 
>    class Event(models.Model):
>  File "app\events\models.py", line 14, in Event
>    job = models.ForeignKey(Job, blank=False, null=False)
> NameError: name 'Job' is not defined
>
> It's like Django doesn't like the cross import between Job and Event.
> Anyone can tell me what I'm doing wrong ?
> I think the easy solution is to declar the Event class in the Job
> model file but I'd like to keep them in different files.

One solution to this is described in the relevant documentation:

http://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey

-- 
Ramiro Morales
http://rmorales.net

PyCon 2009 Argentina - Vie 4 y Sab 5 Septiembre
Buenos Aires, Argentina
http://ar.pycon.org/2009/about/

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



NameError: name 'Job' is not defined

2009-07-10 Thread bruno

Hi,

I'm new in django and I have problem with importing models

I have two class : Job and Event

The Job class defines a method run_job() wich do something and log the
result by creating a new Event so I need to import the Event class
from file app/events/models.py
The Event class reference the job who created the log entry so I need
to import the Job class from file app/jobs/models.py


file : app/jobs/models.py
--

from events.models import Event
...

class Job(models.Model):
...
def run_job(self):
result = do_something()
e = Event(timestamp_gmt=datetime.now(), job=self,
result=result)
self.save()
return result


file : app/events/models.py
--

from jobs.models import Job
...

class Event(models.Model):
...
timestamp_gmt = models.DateTimeField(null=False, editable=False)
job = models.ForeignKey(Job, blank=False, null=False)


When I start the serveur I have the following error :
...
  File "app\jobs\models.py", line 8, in 
from events.models import Event
  File "app\events\models.py", line 12, in 
class Event(models.Model):
  File "app\events\models.py", line 14, in Event
job = models.ForeignKey(Job, blank=False, null=False)
NameError: name 'Job' is not defined

It's like Django doesn't like the cross import between Job and Event.
Anyone can tell me what I'm doing wrong ?
I think the easy solution is to declar the Event class in the Job
model file but I'd like to keep them in different files.

Thanks

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---