empty ImageField's in _post_save method

26 views
Skip to first unread message

Krzysztof Drozd

unread,
Sep 4, 2005, 11:38:27 PM9/4/05
to Django users
i have wrote some app thats have 3 ImageFields.
- model -
name = meta.CharField(maxlength=33)
basic_file = meta.ImageField(blank=False,null=False)
small_file = meta.ImageField(blank=True)
biggg_file = meta.ImageField(blank=True)

in my _post_save(self):
import Image
from project.settings.main import MEDIA_ROOT
ib = Image.open(MEDIA_ROOT,'/'+self.basic_file)
#
# make thumb,..

all another fields in this model are not empty,
only the ImageFields !!!

how to can i acces uploaded file to edit them?
who is it?
why ImageFields are empty in _post_save() ??

thanks kd.

ps my django is r621

Krzysztof Drozd

unread,
Sep 4, 2005, 11:41:58 PM9/4/05
to Django users
pps: get_basic_file_filename() is eq to MEDIA_ROOT :( it contains only
a dir, not the path to the file

Adrian Holovaty

unread,
Sep 5, 2005, 11:04:46 AM9/5/05
to django...@googlegroups.com
On 9/4/05, Krzysztof Drozd <krzy...@asi.pwr.wroc.pl> wrote:
> i have wrote some app thats have 3 ImageFields.
> - model -
> name = meta.CharField(maxlength=33)
> basic_file = meta.ImageField(blank=False,null=False)
> small_file = meta.ImageField(blank=True)
> biggg_file = meta.ImageField(blank=True)
>
> in my _post_save(self):
> import Image
> from project.settings.main import MEDIA_ROOT
> ib = Image.open(MEDIA_ROOT,'/'+self.basic_file)
> #
> # make thumb,..
>
> all another fields in this model are not empty,
> only the ImageFields !!!

I suspect things aren't working because you haven't set the required
"upload_to" attribute on your ImageFields. See the FileField
documentation at http://www.djangoproject.com/documentation/model_api/
.

I've updated the model validator to throw an error if upload_to isn't
given for FileFields or ImageFields.

Adrian
--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Krzysztof Drozd

unread,
Sep 5, 2005, 11:33:03 AM9/5/05
to Django users
this is not a problem, if the upload_to attribute is not given django
will uplada the file in MEDIA_ROOT directory and make that.

i can uplad file and servin him from django. but when i will resize
(with PIL) the upladed file i con't.

when i make
im = Image.open(PATH_TO_IMAGE+self.ImageFieldName)
the self.ImageFieldName is Null/''
without this line all is ok !!!

mayby you can wrot a example for me: how to resize or rotate a image
thats bean just uploaded?

ther is my model, im come from poland and labels are in polish.
this will be a wery semple gallery. i wont upload a {{plik}} and make
a thumb = {{male}} and a preview = {{duze}} in _post_save()

# -*- coding: utf-8 -*-
from django.core import meta
from aparatura.settings.main import MEDIA_ROOT

class galeria(meta.Model):
nazwa = meta.CharField(maxlength=64)
opis = meta.TextField()
class META:
admin = meta.Admin(
js =
('/media/js/tiny_mce/tiny_mce.js','/media/js/tiny_mce/textareas.js'),
)
def __repr__(self):
return self.nazwa

class zdjecie(meta.Model):
galeria = meta.ForeignKey(galeria)
nazwa = meta.CharField(maxlength=64)
opis = meta.TextField()
plik =
meta.ImageField(blank=False,null=False,upload_to=MEDIA_ROOT+'/zdjecia/')
male = meta.ImageField(blank=True,default='brak.png')
duze = meta.ImageField(blank=True,default='brak.png')

class META:
admin = meta.Admin(
js =
('/media/js/tiny_mce/tiny_mce.js','/media/js/tiny_mce/textareas.js'),
list_display = ('nazwa', 'opis', 'plik','male','duze'),
)
def __repr__(self):
return self.nazwa

def _pre_save(self):
self.male=self.plik+'.male.png'
self.duze=self.plik+'.duze.png'
self.nazwa = self.plik

def _post_save(self):
from aparatura.settings.main import MEDIA_ROOT
from PIL import Image
i8 = open(self.get_plik_filename()+self.plik)
x1 = 100
x2 = 640
y1 = im.size[1]/(im.size[0]/x1)
y2 = im.size[1]/(im.size[0]/x2)
i1 = im.resize((x1,y1),Image.ANTIALIAS)
i2 = im.resize((x2,y2),Image.ANTIALIAS)
i1.save(MEDIA_ROOT+'/'+self.male,'PNG')
i2.save(MEDIA_ROOT+'/'+self.duze,'PNG')

Reply all
Reply to author
Forward
0 new messages