Override Django inlineformset_factory has_changed () to always return True

I am using django's inlineformset_factory function.

a = get_object_or_404(ModelA, pk=id)

FormSet = inlineformset_factory(ModelA, ModelB)
if request.method == 'POST':
    metaform = FormSet (instance=a, data=request.POST)

    if metaform.is_valid():
        f = metaform.save(commit=False)

        for instance in f:
           instance.updated_by = request.user
           instance.save()
else:
    metaform = FormSet(instance=a)

return render_to_response('nodes/form.html', {'form':metaform})

      

What happens is that if I change any of the data, then everything works fine and all the data is updated. However, if I don't change any of the data, the data is not updated. that is, only records that change go through a for loop to save. I guess this makes sense as there is no space saving data if it hasn't changed. However, I need to go through and save each object in the form, whether there are any changes on it.

So my question is, how do I override this so that it goes through and retains every entry regardless of whether there are any changes or not?

I hope this makes sense

thanks

+2


a source to share


2 answers


inlineformset_factory can, I think, accept a form object. I suppose what you can do is create a .ModelForm and then add a type field always_update = forms.IntegerField(required=False)

and then in a function __init__

do something like self.fields['always_update'].initial = int(time.time())

. I believe this will force it to update, but you'll have to test that.



+3


a source


If the field is updated_by

always going to be the same for every ModelB instance associated with a particular ModelA instance, shouldn't you just store it once on the parent instead of every child?



0


a source







All Articles