Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django model - on_delete=models.PROTECT()

I'm trying to use on_delete with my models but my IDE is asking me for: collector, fields, sub_objs, using (i.e. ..., on_delete=models.PROTECT(collector, fields, sub_objs, using)).

Can someone please tell me what these are and give me a quick example because I can find them documented anywhere :(

like image 625
Robert Johnstone Avatar asked May 18 '11 17:05

Robert Johnstone


2 Answers

Ignore your IDE. It is trying to get you to call the models.PROTECT function, which does indeed take those arguments. But you actually want to pass the function itself:

my_field = models.ForeignKey(..., on_delete=models.PROTECT)

ie without the parentheses that would call the function.

(Insert rant about using an IDE with a dynamic language here...)

like image 197
Daniel Roseman Avatar answered Sep 27 '22 16:09

Daniel Roseman


Import like:(Python 2.7)

from django.db.models.deletion import PROTECT

Then you can use it directly.

category = ForeignKey(TCategory, PROTECT, null=False, blank=False)
like image 42
WesternGun Avatar answered Sep 27 '22 17:09

WesternGun