Reputation: 3325
I have this two models in my models.py archive:
class Ticket(models.Model):
name = models.CharField(max_length=50)
description_issue = models.CharField(max_length=1000)
pub_date = models.DateTimeField("publication date", default=timezone.now)
class PossibleSolution(models.Model):
title = models.CharField(max_length=50)
description_solution = models.CharField(max_length=1000)
final = models.BooleanField()
sol_date = models.DateTimeField("solution date", default=timezone.now)
tickets = models.ForeignKey(Ticket, blank=True, null=True)
Example: I have a Ticket, with a name, description and the publication date, and then I create a PossibleSolution, and in the form from which I create the PossibleSolution, I choose the related ticket.
Then, in another form, I update the original Ticket, changing it's name, and then I lose the PossibleSolution I created.
What can be happening here?
EDIT: This is the from from where I edit the Ticket
class TicketForm(forms.ModelForm):
class Meta:
def __init__(self, *args, **kwargs):
super(TicketForm, self).__init__(*args, **kwargs)
self.fields['pub_date'].widget = widgets.AdminSplitDateTime()
self.fields['closing_date'].widget = widgets.AdminSplitDateTime()
self.fields['issuer'].widget.attrs['readonly'] = True
model = Ticket
fields = '__all__'
labels = {
'name': _('Nombre'),
'description_issue': _('Descripción'),
'pub_date': _('Fecha de creacion'),
'closing_date': _('Fecha de cierre'),
'priority': _('Prioridad'),
'issuer': _('Creado por'),
'category': _('Categoría')
}
widgets = {
'pub_date': DateTimeWidget(attrs={'id': "pub_date"}, usel10n=True, bootstrap_version=3),
'closing_date': DateTimeWidget(attrs={'id': "closing_date"}, usel10n=True, bootstrap_version=3),
}
And this is the view where I have the POST
def ticket(request, id_ticket):
if request.method == "POST":
form = TicketForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.save()
messages.success(request, 'ticket-saved')
if id_ticket and int(id_ticket) > 0:
# I have this because I don't know how to really modify
Ticket.objects.get(id=id_ticket).delete()
return redirect('index')
Upvotes: 1
Views: 760
Reputation: 9235
Remove the line which performs the delete query,
def ticket(request, id_ticket):
ticket = Ticket.objects.get(id=id_ticket)
if request.method == "POST":
form = TicketForm(request.POST, instance=ticket)
if form.is_valid():
post = form.save(commit=False)
post.save()
messages.success(request, 'ticket-saved')
return redirect('index')
else:
form = TicketForm(instance=ticket)
return render(request, 'template_name', {'form':form})
Upvotes: 3