Clint Ecker
unread,Mar 14, 2006, 10:47:32 AM3/14/06Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com
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