This is due to magic that django performs on models. The class you defined lands up in some other module that django created, and module level imports are not passed along. You can solve your problem either by importing within the _pre_save, or define your Meta class as:
class Meta:
module_constants = globals()
On 3/15/06, Clint Ecker <[EMAIL PROTECTED]> wrote:
I'm using Django 0.91-svn if that mattersOn 3/14/06, Clint Ecker < [EMAIL PROTECTED]> wrote:I'm having an odd problem when trying to import some python modules in my Django models. Things just aren't working how I'd expect:
The broken model is below. The problem occurs when _post_save is called, it will error out on the "thumbs_dir = os.path..." line because it says that "os" is not globally defined. This doesn't seem to jive with all my other python experience. If I import the module at the top of my file, I should be able to use it all over the place. To fix the problem, I can move the "from django.conf.settings import MEDIA_ROOT
import os.path
import Image" inside the _post_save(self) method. Any ideas why my conventional python wisdom is failing me here?
### Begin Broken Links Model ###
from django.core import meta
from django.conf.settings import MEDIA_ROOT
import os.path
import Image
class Link(meta.Model):
uri = meta.URLField()
link_name = meta.CharField(maxlength=255)
source_name = meta.CharField(maxlength=255)
clicks = meta.IntegerField(default=0)
pub_date = meta.DateTimeField()
thumbnail = meta.ImageField(upload_to="uploads")
approved = meta.BooleanField(default=False)
def __repr__(self):
return self.uri
def _post_save(self):
size = 100, 150
thumbs_dir = os.path.dirname(self.get_thumbnail_filename()) + "/thumbnails/"
thumbsfile = thumbs_dir + "tn_" + os.path.basename(self.get_thumbnail_filename())
infile = self.get_thumbnail_filename()
outfile = thumbsfile
im = Image.open(infile)
im.thumbnail(size)
im.save(outfile,quality=100)
class META:
admin = meta.Admin()
def get_thumb_url(self):
import os.path
tnd = "/thumbnails/"
tnp = "tn_"
fs = self.get_thumbnail_url ()
return os.path.dirname(fs) + tnd + tnp + os.path.basename(fs)
### End Broken Links Model ###
Thanks,Clint
--
Amit Upadhyay
Blog: http://www.rootshell.be/~upadhyay
+91-9867-359-701
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---