InlineFormset Updating issues, after changing the value in the input

8 views
Skip to first unread message

Paul

unread,
Jan 18, 2018, 5:59:56 AM1/18/18
to Django users


I have 2 models Product and Image:

class Image(models.Model):
    product
= models.ForeignKey(Product, related_name='images', on_delete=models.CASCADE)
    parent
= models.ForeignKey('self', blank=True, null=True, verbose_name='original image', on_delete=models.CASCADE)
    image
= models.ImageField(upload_to=file_upload_to)
    image_type
= models.CharField(max_length=50)



parent is a Foreign Key to the original image

I use the Image Model in a formset:

ImageFormSet = inlineformset_factory(parent_model=Product, model=Image,
form
=ImageModelForm, min_num=1, max_num=4, can_delete=True)



Steps:

    Set the Product Form with the inlineformset for Image
    Show and submit the creation form
    save the original image(s)
    after the original image is saved, create multiple image by cropping the original image, using a task
    Edit/Update Form, show as image one of the cropped image (instead of original) by modifying(filter by image_type) the Formset queryset

An here appear the issue:

The {{form.id}} value in case of update, is the pk(id) of the cropped image, instead of the original image:

<input name="product-image-0-id" value="15" type="hidden">



and when the image is updated, is replacing the cropped image in database instead of the original image, and the 'cropped' tasks after and insertion in database is wrong.

I tried to replace the value(using a custom widget) instead of the cropped image with the pk(id) of the original(parent_id).

but if I do, this, on updating/replacing an older image, the new replacing image, is ignored; the form submit with no errors, but the replace image is not replaced; the other new images are ok.

So I think is a mechanism that compares the value in the the input with what was passed to the form, and if doesn't correspond is jut ignoring.

Reply all
Reply to author
Forward
0 new messages