There is any way to prevent cascade delete on FK field, without using signal?

101 views
Skip to first unread message

Fellipe Henrique

unread,
Oct 3, 2017, 12:30:26 PM10/3/17
to Django Users
There is any way to prevent cascade delete on FK field, without using signal?

I need to block the Delete process, not to set NULL on field.. 

Regards!


T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

Matthew Pava

unread,
Oct 3, 2017, 12:40:20 PM10/3/17
to django...@googlegroups.com

Have you checked the arguments for on_delete on ForeignKey?

https://docs.djangoproject.com/en/1.11/ref/models/fields/#arguments

 

There’s models.SET_NULL, models.SET_DEFAULT, models.DO_NOTHING, etc…

 

If you need to prevent someone from deleting something, you should use view permissions.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF1jwZH4s%3DSnCaS3hqCiZPQd%3D4vvZPYU1EH7oEp1ixpNrg5ERw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Fabio C. Barrionuevo da Luz

unread,
Oct 3, 2017, 12:40:26 PM10/3/17
to django...@googlegroups.com
explicitly define what to do when there is a deletion.

use the "on_delete" parameter of ForeignKey/OneToOneField

on django <=1.11, on_delete is optional, and its default value is CASCADE.

after django >- 2.0, it is mandatory to set "on_delete"


https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey.on_delete



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.



--
Fábio C. Barrionuevo da Luz
Palmas - Tocantins - Brasil - América do Sul


Blog colaborativo sobre Python e tecnologias Relacionadas, mantido totalmente no https://github.com/pythonclub/pythonclub.github.io .

Todos são livres para publicar. É só fazer fork, escrever sua postagem e mandar o pull-request. Leia mais sobre como publicar em README.md e contributing.md.
Regra básica de postagem:
"Você" acha interessante? É útil para "você"? Pode ser utilizado com Python ou é útil para quem usa Python? Está esperando o que? Publica logo, que estou louco para ler...

Fellipe Henrique

unread,
Oct 4, 2017, 7:37:38 AM10/4/17
to Django Users
I read the docs, but doesn't work for me... still delete the record... I looking something directly to model, not using view... there's any way to do that?

T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

Matthew Pava

unread,
Oct 4, 2017, 9:36:35 AM10/4/17
to django...@googlegroups.com

Hi Felipe,

I found this question on Stack Overflow, which seems to provide some insight on how to accomplish what you want.

https://stackoverflow.com/questions/4825815/prevent-delete-in-django-model

Basically, you need to override the delete method on the model and on the QuerySet of the model’s managers.  Take a look at the last answer.

 

Still, I’m curious why setting the on_delete parameter to DO_NOTHING doesn’t work for you.  Are you overriding something in your code?

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Fellipe Henrique


Sent: Wednesday, October 4, 2017 6:36 AM
To: Django Users

--

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.



 

--

Fábio C. Barrionuevo da Luz
Palmas - Tocantins - Brasil - América do Sul

 

 

Blog colaborativo sobre Python e tecnologias Relacionadas, mantido totalmente no https://github.com/pythonclub/pythonclub.github.io .

 

Todos são livres para publicar. É só fazer fork, escrever sua postagem e mandar o pull-request. Leia mais sobre como publicar em README.md e contributing.md.

Regra básica de postagem:

"Você" acha interessante? É útil para "você"? Pode ser utilizado com Python ou é útil para quem usa Python? Está esperando o que? Publica logo, que estou louco para ler...

 

--
You received this message because you are subscribed to the Google Groups "Django users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

 

--

You received this message because you are subscribed to the Google Groups "Django users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.


To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

James Schneider

unread,
Oct 5, 2017, 12:16:21 AM10/5/17
to django...@googlegroups.com


On Oct 4, 2017 4:36 AM, "Fellipe Henrique" <fell...@gmail.com> wrote:
I read the docs, but doesn't work for me... still delete the record... I looking something directly to model, not using view... there's any way to do that?

What exactly did you "do"? The on_delete keyword is applied at the model level, not the view.

-James
Reply all
Reply to author
Forward
0 new messages