I have a creation-form for “event” like
class EventCreationForm(forms.ModelForm):
guests_emails = forms.CharField(
required=False,
widget=forms.HiddenInput
(),
)
hosting_date = forms.DateTimeField(input_formats=["%d-%m-%Y %H:%M"], widget=forms.DateTimeInput(format="%d-%m-%Y %H:%M", attrs={"placeholder": "dd-mm-yyyy T:M"}))
class Meta:
model = BaseEvent
fields = ["description", "title", "hosting_date", "place", "cover_image"]
def clean_guests_emails(self):
emails = self.cleaned_data["guests_emails"] or "[]"
emails = json.loads(emails)
for email in emails:
validate_email(email)
return emails
and a view for updating an instance
@login_required
def edit_event(request, pk):
event = get_object_or_404(BaseEvent, pk=pk)
# Ensure only hosts can edit the event
if request.user not in event.hosts.all():
return redirect('event_detail', pk=event.pk) # Redirect if user is not a host
if request.method == 'POST':
form = EventCreationForm(request.POST, instance=event)
if form.is_valid():
form.save()
return redirect('event_detail', pk=event.pk) # Redirect to event detail page
else:
form = EventCreationForm(instance=event)
return render(request, 'events/edit_event.html', {'form': form, 'event': event})
The form.save()
is being called but for some reason the instance is not updated.
If I do it on the instance i.e
#form.save()
instance = form.save(commit=False)
instance.save()
then it works fine.
I’m not getting any error, the form contains the changes etc., thus I can’t see why the form.save()
won’t work (especially when using it to create an instance and then save that works).
Note, I’m not overwriting the save()
method anywhere.
Any ideas