Reputation: 23
The error below occurs when attempting to create a comment.
And my code is as follows. I want to know the solution.
Help me. I'm such a beginner.
models.py
class Board(models.Model):
b_no = models.AutoField(primary_key=True)
b_title = models.CharField(max_length=255)
b_note = models.TextField(null=True, help_text="")
b_writer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
parent_no = models.IntegerField(default=0, null=True)
b_date = models.DateTimeField(auto_now_add = True, blank=True, null=True)
b_count = models.PositiveIntegerField(default=0)
usage_flag = models.CharField(null=True, max_length=10, default='1')
class Comment(models.Model):
Board = models.ForeignKey(Board, on_delete=models.CASCADE)
c_writer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
c_note = models.TextField(null=True, help_text="")
c_date = models.DateTimeField(auto_now_add = True, blank=True, null=True)
serializers.py
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = ['c_writer', 'c_note', 'c_date']
class CommentCreateSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = ['c_writer', 'c_note']
views.py
class CommentCreateView(CreateAPIView):
# lookup_field = 'no'
queryset = Comment.objects.all()
serializer_class = CommentCreateSerializer
# def form_vaild(self, form):
# comment = form.save(commit=False)
# comment.writer = self.request.user
# comment.board = get_object_or_404(Board, pk=self.kwargs['board_pk'])
# return super().form_valid(form)
class CommentDeleteView(DestroyAPIView):
# lookup_field = 'no'
queryset = Comment.objects.all()
serializer_class = CommentSerializer
urls.py
path('boardapi/<int:board_pk>/comment/create/', views.CommentCreateView.as_view(), name='CommentCreateView'),
path('boardapi/<int:board_pk>/comment/<int:pk>/delete/', views.CommentDeleteView.as_view(), name='CommentDeleteView'),
Upvotes: 0
Views: 105
Reputation: 19
This article is helpful for you please read. Class Based Views While functions are easy to work with, it’s often beneficial to use class based views to reuse functionality, especially for large APIs with a number of endpoints. https://realpython.com/django-rest-framework-class-based-views/
Upvotes: 0