I have multiple Product
objects in my project. I want to create a form which updates all Product
objects at once. Since Product
has many attributes I want to spare some time and do it using ModelForm
but I can't figure out how to do that.
So there is no particular object I want to update, instead there is many of these objects but I want to set the same attributes for all but not change id
(and one other field - OneToOne
).
I think this might be a solution but can't finish save
method.
class UpdateMultipleProductObjects(forms.ModelForm):
class Meta:
model = Product
fields = '__all__'
exclude = ['id',...]
def save(self, *args, **kwargs):
temporary_object = super(UpdateMultipleProductObjects,self).save(commit=False,*args,*kwargs)
update_dict = {x.name : getattr(temporary_object,x.name) for x in temporary_object ._meta.fields if x.name not in ['id',...]}
Product.objects.all().update(**update_dict)
Do you have any ideas or better/safer solution?