django override delete cascade for one time
up vote
2
down vote
favorite
Is there an option for to make sure that a model instance does not have any related objects?
i.e, if the Person object has any related objects, I want this line
person.delete()
to raise an error.
And I don't want to modify on_delete=models.CASCADE
for every foreign key. I need this protection only here, for any other case in my application (like django admin site) I do prefer the cascading behavior.
python django django-models django-queryset
add a comment |
up vote
2
down vote
favorite
Is there an option for to make sure that a model instance does not have any related objects?
i.e, if the Person object has any related objects, I want this line
person.delete()
to raise an error.
And I don't want to modify on_delete=models.CASCADE
for every foreign key. I need this protection only here, for any other case in my application (like django admin site) I do prefer the cascading behavior.
python django django-models django-queryset
1
Do you try to overrife delete method from your model? check for your fk and others inside this method and call super(...).delete() to delete your model
– Bast
Nov 8 at 9:42
I didn't override the delete method
– user3599803
Nov 8 at 12:52
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Is there an option for to make sure that a model instance does not have any related objects?
i.e, if the Person object has any related objects, I want this line
person.delete()
to raise an error.
And I don't want to modify on_delete=models.CASCADE
for every foreign key. I need this protection only here, for any other case in my application (like django admin site) I do prefer the cascading behavior.
python django django-models django-queryset
Is there an option for to make sure that a model instance does not have any related objects?
i.e, if the Person object has any related objects, I want this line
person.delete()
to raise an error.
And I don't want to modify on_delete=models.CASCADE
for every foreign key. I need this protection only here, for any other case in my application (like django admin site) I do prefer the cascading behavior.
python django django-models django-queryset
python django django-models django-queryset
asked Nov 8 at 9:35
user3599803
77831337
77831337
1
Do you try to overrife delete method from your model? check for your fk and others inside this method and call super(...).delete() to delete your model
– Bast
Nov 8 at 9:42
I didn't override the delete method
– user3599803
Nov 8 at 12:52
add a comment |
1
Do you try to overrife delete method from your model? check for your fk and others inside this method and call super(...).delete() to delete your model
– Bast
Nov 8 at 9:42
I didn't override the delete method
– user3599803
Nov 8 at 12:52
1
1
Do you try to overrife delete method from your model? check for your fk and others inside this method and call super(...).delete() to delete your model
– Bast
Nov 8 at 9:42
Do you try to overrife delete method from your model? check for your fk and others inside this method and call super(...).delete() to delete your model
– Bast
Nov 8 at 9:42
I didn't override the delete method
– user3599803
Nov 8 at 12:52
I didn't override the delete method
– user3599803
Nov 8 at 12:52
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Does this correspond to what you want?
has_related = False
for field in person.__class__._meta.get_fields():
if field.is_relation:
field_name = field.get_accessor_name()
model_field = getattr(person, field_name)
if not isinstance(model_field, models.Model) and model_field.all():
has_related = True
break
if not has_related:
person.delete()
Thanks, why the use of get_cache_name() for field objects? I'm getting an AttributeError
– user3599803
Nov 8 at 12:52
I updated my post withget_accessor_name()
instead ofget_cache_name()
. We use this method to get theXXX_set
(or else if modified) name of the related field. Where do you get your AttributeError ?
– mistiru
Nov 8 at 13:49
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Does this correspond to what you want?
has_related = False
for field in person.__class__._meta.get_fields():
if field.is_relation:
field_name = field.get_accessor_name()
model_field = getattr(person, field_name)
if not isinstance(model_field, models.Model) and model_field.all():
has_related = True
break
if not has_related:
person.delete()
Thanks, why the use of get_cache_name() for field objects? I'm getting an AttributeError
– user3599803
Nov 8 at 12:52
I updated my post withget_accessor_name()
instead ofget_cache_name()
. We use this method to get theXXX_set
(or else if modified) name of the related field. Where do you get your AttributeError ?
– mistiru
Nov 8 at 13:49
add a comment |
up vote
1
down vote
accepted
Does this correspond to what you want?
has_related = False
for field in person.__class__._meta.get_fields():
if field.is_relation:
field_name = field.get_accessor_name()
model_field = getattr(person, field_name)
if not isinstance(model_field, models.Model) and model_field.all():
has_related = True
break
if not has_related:
person.delete()
Thanks, why the use of get_cache_name() for field objects? I'm getting an AttributeError
– user3599803
Nov 8 at 12:52
I updated my post withget_accessor_name()
instead ofget_cache_name()
. We use this method to get theXXX_set
(or else if modified) name of the related field. Where do you get your AttributeError ?
– mistiru
Nov 8 at 13:49
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Does this correspond to what you want?
has_related = False
for field in person.__class__._meta.get_fields():
if field.is_relation:
field_name = field.get_accessor_name()
model_field = getattr(person, field_name)
if not isinstance(model_field, models.Model) and model_field.all():
has_related = True
break
if not has_related:
person.delete()
Does this correspond to what you want?
has_related = False
for field in person.__class__._meta.get_fields():
if field.is_relation:
field_name = field.get_accessor_name()
model_field = getattr(person, field_name)
if not isinstance(model_field, models.Model) and model_field.all():
has_related = True
break
if not has_related:
person.delete()
edited Nov 8 at 13:47
answered Nov 8 at 11:06
mistiru
39512
39512
Thanks, why the use of get_cache_name() for field objects? I'm getting an AttributeError
– user3599803
Nov 8 at 12:52
I updated my post withget_accessor_name()
instead ofget_cache_name()
. We use this method to get theXXX_set
(or else if modified) name of the related field. Where do you get your AttributeError ?
– mistiru
Nov 8 at 13:49
add a comment |
Thanks, why the use of get_cache_name() for field objects? I'm getting an AttributeError
– user3599803
Nov 8 at 12:52
I updated my post withget_accessor_name()
instead ofget_cache_name()
. We use this method to get theXXX_set
(or else if modified) name of the related field. Where do you get your AttributeError ?
– mistiru
Nov 8 at 13:49
Thanks, why the use of get_cache_name() for field objects? I'm getting an AttributeError
– user3599803
Nov 8 at 12:52
Thanks, why the use of get_cache_name() for field objects? I'm getting an AttributeError
– user3599803
Nov 8 at 12:52
I updated my post with
get_accessor_name()
instead of get_cache_name()
. We use this method to get the XXX_set
(or else if modified) name of the related field. Where do you get your AttributeError ?– mistiru
Nov 8 at 13:49
I updated my post with
get_accessor_name()
instead of get_cache_name()
. We use this method to get the XXX_set
(or else if modified) name of the related field. Where do you get your AttributeError ?– mistiru
Nov 8 at 13:49
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204945%2fdjango-override-delete-cascade-for-one-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Do you try to overrife delete method from your model? check for your fk and others inside this method and call super(...).delete() to delete your model
– Bast
Nov 8 at 9:42
I didn't override the delete method
– user3599803
Nov 8 at 12:52