I'm struggling with getting _pre_save() or _post_save() to work. I will
have prose description followed by code.
What I'm Using
==============
Trunk version of Django 0.91.x
What I'm Trying To Do
=====================
There are multiple images associated with a master asset. Each image
has a pre-defined aspect ratio (which is a lookup). I would like for
the aspect ratio association to happen when the image is uploaded,
since we can calculate that based on width and height.
Models
======
class Asset(meta.Model):
asset_id = meta.IntegerField(unique=True)
def __repr__(self):
return str(self.asset_id)
class META:
admin = meta.Admin()
class AspectRatio(meta.Model):
label = meta.CharField(maxlength=20)
width = meta.IntegerField()
height = meta.IntegerField()
ratio = meta.FloatField(max_digits=3, decimal_places=2)
def __repr__(self):
return self.label
class META:
admin = meta.Admin()
class Framegrab(meta.Model):
asset_id = meta.ForeignKey(
Asset,
edit_inline=meta.TABULAR,
num_in_admin=8,
min_num_in_admin=8,
num_extra_on_change=4
)
img = meta.ImageField(
'full size frame grab',
core=True,
upload_to = 'fgs/data/images/%Y_%m_%d',
height_field = 'img_height',
width_field = 'img_width'
)
img_height = meta.IntegerField(editable=False, null=True)
img_width = meta.IntegerField(editable=False, null=True)
upload_time = meta.DateTimeField(
'upload date & time',
default=meta.LazyDate(),
editable=False,
blank=True
)
ratio = meta.ForeignKey(AspectRatio, editable=False)
# Internal helper methods
def _setRatio(self):
from django.models.fgs import aspectratios
ratio = round( float(self.img_width) / self.img_height, 2 )
try:
r = aspectratios.get_object(ratio__exact=ratio)
except:
r = aspectratios.get_object(ratio__exact=0.0)
self.ratio_id = r.id
# modifier class methods
def _pre_save(self):
if self.img and self.img_height:
self._setRatio()
def __repr__(self):
from os.path import split
return split(self.get_img_filename())[1]
class META:
admin = meta.Admin()
What happens
============
Adding or updating an asset without any framegrabs is fine. Uploading
just one framegrab is fine, but any more and I get essentially four
blank framegrab records.
Adding framegrabs directly by using "Add Framegrabs" works as well. But
the use case is for users to add mulitple framegrabs at a time, so it
would be a lot more convenient.
Any ideas?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---