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')
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...
– ElvisDba
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.
– Ernesto Casanova
@Ernestocasanova, see if you can help him with his next answers: https://pt.meta.stackoverflow.com/a/6972/137387
– Augusto Vasques
@Ernestocasanova - good afternoon, there would be the possibility to help me once again in this post - Save report in Django Excel with filter
– ElvisDba
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?
– Ernesto Casanova
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.
– Ernesto Casanova