class ImageStore(models.Model):
img = models.ImageField('image', max_length=1000,
height_field='height', width_field='width',
upload_to='img/uploads/%Y/%m/%d', blank=True, null=True)
height = models.PositiveIntegerField(null=True, blank=True,
editable=False)
width = models.PositiveIntegerField(null=True, blank=True,
editable=False)
alt = models.CharField('alter/title text', max_length=255)
product = models.ForeignKey(Product)
class ImageForm(forms.ModelForm):
class Meta:
model = ImageStore
def __init__(self, product=None, *args, **kwargs):
super(ImageForm, self).__init__(*args, **kwargs)
if product is not None:
self.product = product
del self.fields['product']
def save(self, id=None, commit=True):
if id is None:
instance = ImageStore(product=self.product)
else:
instance = ImageStore.objects.get(pk=id)
return forms.save_instance(self, instance, commit=commit)