'User' object has no attribute 'soft_delete' - Django Soft Deletion
i have created soft delete in my project and it is working fine. But the problem is that i am using User
in-built model and OneToOneField
with UserProfile
. Now soft_delete
function is in UserProfile
Model where im using generic.DeleteView
to delete user. The problem is that i could not pass object of User
to UserProfile
to set user_is_deleted
to True
. Here is my Code.
Views.py
class UserDeleteView(LoginRequiredMixin, generic.DeleteView):
model = User
template_name = 'users/user_confirm_delete.html'
success_url = '/users/'
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.soft_delete()
return HttpResponseRedirect(self.get_success_url())
Models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_company = models.ForeignKey(Company, on_delete=models.CASCADE)
user_role = models.ForeignKey(Roles, on_delete=models.CASCADE)
user_is_deleted = models.BooleanField(default=False)
user_deleted_at = models.DateTimeField(blank=True, null=True)
def soft_delete(self):
self.user_is_deleted = True
self.user_deleted_at = timezone.now() - tdelta(days=-1)
self.save()
Soft Delete Reference - https://blog.khophi.co/soft-delete-django-quickly/
django django-models django-forms django-templates django-views
add a comment |
i have created soft delete in my project and it is working fine. But the problem is that i am using User
in-built model and OneToOneField
with UserProfile
. Now soft_delete
function is in UserProfile
Model where im using generic.DeleteView
to delete user. The problem is that i could not pass object of User
to UserProfile
to set user_is_deleted
to True
. Here is my Code.
Views.py
class UserDeleteView(LoginRequiredMixin, generic.DeleteView):
model = User
template_name = 'users/user_confirm_delete.html'
success_url = '/users/'
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.soft_delete()
return HttpResponseRedirect(self.get_success_url())
Models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_company = models.ForeignKey(Company, on_delete=models.CASCADE)
user_role = models.ForeignKey(Roles, on_delete=models.CASCADE)
user_is_deleted = models.BooleanField(default=False)
user_deleted_at = models.DateTimeField(blank=True, null=True)
def soft_delete(self):
self.user_is_deleted = True
self.user_deleted_at = timezone.now() - tdelta(days=-1)
self.save()
Soft Delete Reference - https://blog.khophi.co/soft-delete-django-quickly/
django django-models django-forms django-templates django-views
add a comment |
i have created soft delete in my project and it is working fine. But the problem is that i am using User
in-built model and OneToOneField
with UserProfile
. Now soft_delete
function is in UserProfile
Model where im using generic.DeleteView
to delete user. The problem is that i could not pass object of User
to UserProfile
to set user_is_deleted
to True
. Here is my Code.
Views.py
class UserDeleteView(LoginRequiredMixin, generic.DeleteView):
model = User
template_name = 'users/user_confirm_delete.html'
success_url = '/users/'
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.soft_delete()
return HttpResponseRedirect(self.get_success_url())
Models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_company = models.ForeignKey(Company, on_delete=models.CASCADE)
user_role = models.ForeignKey(Roles, on_delete=models.CASCADE)
user_is_deleted = models.BooleanField(default=False)
user_deleted_at = models.DateTimeField(blank=True, null=True)
def soft_delete(self):
self.user_is_deleted = True
self.user_deleted_at = timezone.now() - tdelta(days=-1)
self.save()
Soft Delete Reference - https://blog.khophi.co/soft-delete-django-quickly/
django django-models django-forms django-templates django-views
i have created soft delete in my project and it is working fine. But the problem is that i am using User
in-built model and OneToOneField
with UserProfile
. Now soft_delete
function is in UserProfile
Model where im using generic.DeleteView
to delete user. The problem is that i could not pass object of User
to UserProfile
to set user_is_deleted
to True
. Here is my Code.
Views.py
class UserDeleteView(LoginRequiredMixin, generic.DeleteView):
model = User
template_name = 'users/user_confirm_delete.html'
success_url = '/users/'
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.soft_delete()
return HttpResponseRedirect(self.get_success_url())
Models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_company = models.ForeignKey(Company, on_delete=models.CASCADE)
user_role = models.ForeignKey(Roles, on_delete=models.CASCADE)
user_is_deleted = models.BooleanField(default=False)
user_deleted_at = models.DateTimeField(blank=True, null=True)
def soft_delete(self):
self.user_is_deleted = True
self.user_deleted_at = timezone.now() - tdelta(days=-1)
self.save()
Soft Delete Reference - https://blog.khophi.co/soft-delete-django-quickly/
django django-models django-forms django-templates django-views
django django-models django-forms django-templates django-views
edited Dec 31 '18 at 8:55
Huzaif Sayyed
asked Dec 31 '18 at 8:37
Huzaif SayyedHuzaif Sayyed
191110
191110
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Change your delete()
method as,
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.userprofile.soft_delete()
return HttpResponseRedirect(self.get_success_url())
@Huzaif Sayyed Thanks for the dit :) Mark the answer if it solved the problem
– JPG
Dec 31 '18 at 8:51
1
yeah sure waiting to accept thanks alot
– Huzaif Sayyed
Dec 31 '18 at 8:52
add a comment |
Change this
@classmethod
def soft_delete(self):
...
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53985278%2fuser-object-has-no-attribute-soft-delete-django-soft-deletion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Change your delete()
method as,
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.userprofile.soft_delete()
return HttpResponseRedirect(self.get_success_url())
@Huzaif Sayyed Thanks for the dit :) Mark the answer if it solved the problem
– JPG
Dec 31 '18 at 8:51
1
yeah sure waiting to accept thanks alot
– Huzaif Sayyed
Dec 31 '18 at 8:52
add a comment |
Change your delete()
method as,
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.userprofile.soft_delete()
return HttpResponseRedirect(self.get_success_url())
@Huzaif Sayyed Thanks for the dit :) Mark the answer if it solved the problem
– JPG
Dec 31 '18 at 8:51
1
yeah sure waiting to accept thanks alot
– Huzaif Sayyed
Dec 31 '18 at 8:52
add a comment |
Change your delete()
method as,
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.userprofile.soft_delete()
return HttpResponseRedirect(self.get_success_url())
Change your delete()
method as,
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.userprofile.soft_delete()
return HttpResponseRedirect(self.get_success_url())
edited Dec 31 '18 at 8:53
answered Dec 31 '18 at 8:45
JPGJPG
13.7k2832
13.7k2832
@Huzaif Sayyed Thanks for the dit :) Mark the answer if it solved the problem
– JPG
Dec 31 '18 at 8:51
1
yeah sure waiting to accept thanks alot
– Huzaif Sayyed
Dec 31 '18 at 8:52
add a comment |
@Huzaif Sayyed Thanks for the dit :) Mark the answer if it solved the problem
– JPG
Dec 31 '18 at 8:51
1
yeah sure waiting to accept thanks alot
– Huzaif Sayyed
Dec 31 '18 at 8:52
@Huzaif Sayyed Thanks for the dit :) Mark the answer if it solved the problem
– JPG
Dec 31 '18 at 8:51
@Huzaif Sayyed Thanks for the dit :) Mark the answer if it solved the problem
– JPG
Dec 31 '18 at 8:51
1
1
yeah sure waiting to accept thanks alot
– Huzaif Sayyed
Dec 31 '18 at 8:52
yeah sure waiting to accept thanks alot
– Huzaif Sayyed
Dec 31 '18 at 8:52
add a comment |
Change this
@classmethod
def soft_delete(self):
...
add a comment |
Change this
@classmethod
def soft_delete(self):
...
add a comment |
Change this
@classmethod
def soft_delete(self):
...
Change this
@classmethod
def soft_delete(self):
...
answered Dec 31 '18 at 8:45
Shafikur RahmanShafikur Rahman
1,75831024
1,75831024
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53985278%2fuser-object-has-no-attribute-soft-delete-django-soft-deletion%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