глючная форма из модели

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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)