'User' object has no attribute 'soft_delete' - Django Soft Deletion












0















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/










share|improve this question





























    0















    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/










    share|improve this question



























      0












      0








      0








      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/










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 31 '18 at 8:55







      Huzaif Sayyed

















      asked Dec 31 '18 at 8:37









      Huzaif SayyedHuzaif Sayyed

      191110




      191110
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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())





          share|improve this answer


























          • @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





















          2














          Change this



          @classmethod
          def soft_delete(self):
          ...





          share|improve this answer























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            1














            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())





            share|improve this answer


























            • @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


















            1














            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())





            share|improve this answer


























            • @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
















            1












            1








            1







            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())





            share|improve this answer















            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())






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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





















            • @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















            2














            Change this



            @classmethod
            def soft_delete(self):
            ...





            share|improve this answer




























              2














              Change this



              @classmethod
              def soft_delete(self):
              ...





              share|improve this answer


























                2












                2








                2







                Change this



                @classmethod
                def soft_delete(self):
                ...





                share|improve this answer













                Change this



                @classmethod
                def soft_delete(self):
                ...






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 31 '18 at 8:45









                Shafikur RahmanShafikur Rahman

                1,75831024




                1,75831024






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas