I have a really weird problem. I have a formset that will only save some data and not others, specifically:
- Any new data is not saved
- Data created before a certain date that is updated is saved properly
- 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}"