I'm absolutely new to Django and for some reason, I had to jump right to Django REST framework. I want to save JSON objects sent as POST requests in my database. The JSON object looks like this:
[{
"title": "Bitcoin Price Likely to Reach $50K Soon According to This Indicator",
"description": "",
"date": "2021-02-09T09:08:58Z",
"link": "https://cryptopotato.com/bitcoin-price-likely-to-reach-50k-soon-according-to-this-indicator/",
"keywords": [{
"name": "bitcoin"
}],
"source": "https://cryptocompare.com"
},
{
"title": "Post-Tesla News FOMO Helps Bitcoin Price to Surge Above $48,000",
"description": "",
"date": "2021-02-09T09:08:58Z",
"link": "https://www.cryptoglobe.com/latest/2021/02/post-tesla-news-fomo-helps-bitcoin-price-to-surge-above-48000/",
"keywords": [{
"name": "bitcoin"
}],
"source": "https://cryptocompare.com"
}]
I created my models like this:
class Keywords(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return self.name
class News(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
date = models.DateTimeField()
link = models.URLField()
keywords = models.ManyToManyField(Keywords)
source = models.CharField(max_length=30)
class Meta:
db_table = "News"
# ordering = "-date"
def __str__(self):
return self.title
serializers:
class KeywordSerializer(serializers.ModelSerializer):
class Meta:
model = Keywords
fields = ["name"]
class NewsSerializer(serializers.ModelSerializer):
keywords = KeywordSerializer(read_only=True, many=True)
class Meta:
model = News
fields = ["title", "description", "date", "link", "keywords", "source"]
and finally my view:
class NewsView(APIView):
def post(self, request):
news_serializer = NewsSerializer(data=request.data, many=True)
try:
if news_serializer.is_valid():
news_serializer.save()
return Response("Created successfully", status=status.HTTP_201_CREATED)
except Exception as e:
print(e)
return Response("Error, Don't know what", status=status.HTTP_400_BAD_REQUEST)
Normally i try to not to post vague and general questions, but in this one, i've got absolutely no idea how to debug and find the problem here. The only thing that i get from terminal is
Bad Request: /news/
could you please point out the problem, and also give a solution on how to fix it?