Я новичок в python, и я хочу сделать API в Django, одна из проблем, с которой я сталкиваюсь, пытается заставить работать конечную точку UPDATE и конечную точку CREATE.
Когда я игнорирую некоторые из требуемыхполя во время ОБНОВЛЕНИЯ это работает, но для метода CREATE это не так, оно выдает следующее сообщение об ошибке:
IntegrityError at /api/articles/comment/4/replies/ null value in column "author_id" violates not-null constraint DETAIL: Failing row contains (2018-09-20 04:24:36.225977+00, 89, English is better, null, null)
Когда я удаляю read_only в сериализаторах, проблема переходит к конечной точке UPDATE, как показано ниже в почтальон :
{
"comment": [
"This field is required."
],
"author": [
"This field is required."
]
}
Ниже приведен мой фрагмент кода:
Мой сериализатор в serializers.py
class RepliesSerializer(serializers.ModelSerializer):
class Meta:
model = Replies
fields = '__all__'
read_only_fields = ('comment', 'author',)
Просмотров в views.py
.
class RepliesView(APIView):
permission_classes = (IsAuthenticated,)
def get_object(self, pk):
try:
return Replies.objects.get(pk=pk)
except Replies.DoesNotExist:
raise Http404
def post(self, request, commentID):
content = request.data
author = request.user.id
content['author'] = author
content['comment'] = commentID
# print('--> ', author)
# print('content ---> ',content)
serializer = RepliesSerializer(data=content)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self,request, commentID):
instance = Replies.objects.all()
serializer = RepliesSerializer(instance, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
def delete(self, request, Id, format=None):
snippet = self.get_object(Id)
snippet.delete()
return Response({"message" : "Reply Deleted Successfully."}, status=status.HTTP_204_NO_CONTENT)
def put(self, request, Id):
content = self.get_object(Id)
serializer = RepliesSerializer(content, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Класс модели в model.py
"""
class to declare an article
model. To be used for all articles
"""
from django.db import models
from django.utils import timezone
from authors.apps.articles.utils import generate_slug
from authors.apps.authentication.models import User
# noinspection SpellCheckingInspection
class Article(models.Model):
"""
A model for an article
"""
slug = models.SlugField(max_length=100, unique=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=255, null=False, blank=False,
error_messages={"required": "Write a short title for your article."})
description = models.TextField(null=False, blank=False,
error_messages={"required": "A description of your post is required."})
body = models.TextField(null=False, blank=False,
error_messages={"required": "You cannot submit an article without body."})
created_at = models.DateTimeField(auto_created=True, auto_now=False, default=timezone.now)
updated_at = models.DateTimeField(auto_created=True, auto_now=False, default=timezone.now)
favorited = models.BooleanField(default=False)
favorites_count = models.IntegerField(default=0)
photo_url = models.CharField(max_length=255, null=True)
def __str__(self):
"""
:return: string
"""
return self.title
def save(self, *args, **kwargs):
"""
override default save() to generate slug
:param args:
:param kwargs:
"""
self.slug = generate_slug(Article, self)
super(Article, self).save(*args, **kwargs)
class Meta:
get_latest_by = 'created_at'
ordering = ['-created_at', 'author']
class Comments(models.Model):
article = models.ForeignKey(Article, related_name='comments', on_delete=models.CASCADE)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField(null=False, blank=False, error_messages={"required": "You cannot submit without a comment."})
created_at = models.DateTimeField(auto_created=True, auto_now=False, default=timezone.now)
def __str__(self):
"""
:return: string
"""
return self.body
class Meta:
get_latest_by = 'created_at'
ordering = ['-created_at']
class Replies(models.Model):
comment = models.ForeignKey(Comments, related_name='replies', on_delete=models.CASCADE)
author = models.ForeignKey(User, related_name='replies', on_delete=models.CASCADE)
content = models.TextField(null=False, blank=False,
error_messages={"required": "You cannot submit without a reply."})
created_at = models.DateTimeField(auto_created=True, auto_now=False, default=timezone.now)
def __str__(self):
"""
:return: string
"""
return self.content
class Meta:
get_latest_by = 'created_at'
ordering = ['-created_at']
КакМогу ли я сделать логику, которая предназначена для ОБНОВЛЕНИЯ и СОЗДАНИЯ, чтобы избежать этой ошибки.