Delete without giving error

Asked

Viewed 111 times

0

I have two models, one has the foreign key in the other.

I wanted to implement a delete, that would prevent her from deleting that key when she was already safe in a relationship.

In the view.py I implemented, but I can’t delete any. I think the error is in if that’s in view.

Models py.

class Horario(models.Model):
    horario = models.CharField('Horário', max_length=11, help_text='Ex= 00:00/00:00')
class Alocar(models.Model):
    data = models.DateField('Data', auto_now=True, blank=True)
    horario = models.ForeignKey(Horario, on_delete=models.PROTECT)
    turma = models.ForeignKey(Turma, on_delete=models.PROTECT)
    sala = models.ForeignKey(Sala, on_delete=models.PROTECT)

view py.

@login_required
def DelHorario(request, id):
    context = {}
    horario = get_object_or_404(Horario, pk=id)

    if request.method == 'POST':
        if horario.id > 0:
            messages.success(request, 'Não pode ser excluído, pois está sendo usado numa relação')
        else:
            horario.delete()
        return redirect('alocar:addhorario')
    context['horario'] = horario
    return render(request, 'alocar/delhorario.html')

1 answer

1


Live the problem is that you cannot effectively delete a record if it exists in another table like fk, it violates referential integrity. Being in Django admin you don’t need to do anything it already validates this and shows a message. However, in custom code you can validate by checking if in the relational table there are records with the operator "select_related".

py views.

@login_required
def DelHorario(request, id):
    values = Alocar.objects.select_related('horario').filter(horario__id=id)
    if request.method == 'POST':
        if values:
            messages.success(request, 'Não pode ser excluído, pois está sendo usado numa relação')
        else:
            horario = get_object_or_404(Horario, pk=id)
            horario.delete()
        return redirect('alocar:addhorario')
    return render(request, 'alocar/delhorario.html')
  • Good day...friend as well as I went to Oce directly to ask for help, and Oce promptly helped me...it is more than an obligation of me to thank you... A lot of help...

  • Good, I’m glad you helped. However, I note that doing for you, I do not know if you understood well what I did, IE, "select_related" is the equivalent of sql Inner Join and filters by fk of the time exstente, if there is no allow eliminate. I don’t know if this project allows it, but there is also the possibility of using classes for CRUD check here https://docs.djangoproject.com/en/3.0/ref/class-based-views/generic-editing/. Finally, define functions/methods in Lower case, the classes are defined thus (Delhorario) In other words, pascalcase, I would evaluate this in your project.

  • 1

    @Ernestocasanova, see if you can help him with his next answers: https://pt.meta.stackoverflow.com/a/6972/137387

  • @Ernestocasanova - good afternoon, there would be the possibility to help me once again in this post - Save report in Django Excel with filter

  • Bom, take a look at this example https://simpleisbetterthancomplex.com/tutorial/2016/07/29/how-to-export-to-excel.html. This is what you need?

  • I have also used this, https://simpleisbetterthancomplex.com/packages/2016/08/11/django-import-export.html. However it is limited to the number of Rows, more than 5K is already a problem. That is, it is a synchronous service, it does not contain a mechanism like a background worker that imports all the records after uploading the file to the server.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.