formset.save() not saving data for valid form

I have a really weird problem. I have a formset that will only save some data and not others, specifically:

  1. Any new data is not saved
  2. Data created before a certain date that is updated is saved properly
  3. Data entered via the admin portal after that date is displayed properly but any changes are not saved; changes revert to what was entered via the portal.

It doesn’t appear to be a timestamp issue because if I enter new data and modify the timestamp to one of the earlier timestamp it still does not update.

I’m wondering if it is because I updated the SQLLite DB using a db management tool by exporting all the data, making the desired changes and importing the changed file. That worked in the past, the only thing I did differently is deleted all the data after export and before import.

Could the django created keys (id) gotten messed up even though I still have the original id numbers in the data?

It worked fine before that.

view

def home(request):
    
    # Create unique prefixes for each element ad develope formset ordered by objective number
                
        formset_list = [ 
            ObjectiveTextFormSet
                        (prefix=f'row-{i}',
                        queryset=ObjectiveText.objects.filter(
                            row=i
                            ).order_by('objective__objective_num')
                            )
            for i in range(1,7)       
        ]

        objheader = [1,2,3,4,5,6,7,8] # Create list to use to render objective header


        # Add the formset to context dictionary
        context = {
            'formset_list': formset_list,
            'objheader': objheader,
            
            
        }
        if request.method == "POST":
            formset_list=[ObjectiveTextFormSet(request.POST,prefix=f'row-{i}',
                        queryset=ObjectiveText.objects.filter(
                            row=i
                            ).order_by('objective__objective_num')
                            )
            for i in range(1,7) ]

            for formset in formset_list:
                if formset.is_valid():
                    formset.save()
                    context = {
                        'formset_list': formset_list,
                        'objheader': objheader,
                    }
                    return render(request, 'ISO22301/home.html', context)
                else:
                    for formset in formset_list:   
                        return render(request, 'ISO22301/home.html', context)
        else:
            for formset in formset_list:
                return render(request, "ISO22301/home.html", context)

formset

ObjectiveTextFormSet = modelformset_factory(ObjectiveText, fields = ["objective_text"], 
                                            extra = 0, widgets={'objective_text': Textarea(attrs={'cols': 15, 'rows': 5})} )

model

class ObjectiveText(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    dashboard=models.ForeignKey(Dashboard, on_delete=models.CASCADE)
    objective = models.ForeignKey(Objectives, on_delete=models.CASCADE) 
    row = models.ForeignKey(Row, on_delete=models.CASCADE)
    objective_position = models.IntegerField()
    objective_text = models.CharField(max_length=1000, blank = True)
    timestamp= models.TimeField(auto_now = True)

    def __str__(self):
        return f" Objective: {self.objective} {self.objective_text} at {self.timestamp}"

The issue here is in your flow of control of the POST section in your view.

You want to iterate over all of the formset in formset_list. However, if your first formset is valid, you’re going to return a response which will exit your for loop. You will never process any valid formsets after the first one.

Thanks. Figured it out - after checking if the formset is valid I loop through the forms in the formset and save the. Since the formset is valid the individual forms are as well.

        if request.method == "POST":
            formset_list=[ObjectiveTextFormSet(request.POST,prefix=f'row-{i}',
                        queryset=ObjectiveText.objects.filter(
                        row=i
                        ).order_by('objective__objective_num'))
                        for i in range(1,7)
                        ]
                    for formset in formset_list:
                        if formset.is_valid():
                            for form in formset:
                                    form.save()
            context = {
                'formset_list': formset_list,
                'objheader': objheader,
            }
            return render(request, 'ISO22301/home.html', context)
        else:
                return render(request, 'ISO22301/home.html', context)