0

Here I am trying to register log if the new category has been created. But this is not working.I am using generic View to create the category model now I want to add LogEntry to this model so tried like this but it is throwing error.

Exception Type: AttributeError
Exception Value:    
'CreateCategory' object has no attribute 'object'  

views

 class CreateCategroy(SuccessMessageMixin, generic.CreateView):
        model = Category
        fields = ['title']
        template_name = 'add_category.html'
        success_url = reverse_lazy('list_category')
        success_message = 'Created Successfully.'

        def post(self, request, *args, **kwargs):
            LogEntry.objects.log_action(user_id=self.request.user.pk,
                                        content_type_id=ContentType.objects.get_for_model(Category).pk,
                                        object_id=self.object.pk,
                                        object_repr=force_text(self.object),
                                        change_message='New Category has been created.',
                                        action_flag=1
                                        )
D_P
  • 802
  • 10
  • 32
  • Is that the full code? I'm assuming the `object` in this case is going to refer to the new `Category`? If so, you'll need to actually create this first so you actually have a reference to it. – michjnich Jun 15 '20 at 07:46
  • yes it's the full code to create the category. Before it worked now I added `LogEntry` model with `post` method then it neither creates the category nor log and throws the error. @urbanespaceman – D_P Jun 15 '20 at 07:51
  • Is this helpful:https://stackoverflow.com/questions/25212745/why-doesnt-self-object-in-a-createview-have-an-id-after-saving-to-the-database – michjnich Jun 15 '20 at 10:14
  • Also, I note your error says "CreateCategory", but your code misspells this as "Categroy" - is this just a typo in your question, or are (if you copy/pasted) you maybe actually calling a different method? – michjnich Jun 15 '20 at 10:15

0 Answers0