Hi guys,
I am working on project in which admin will post news and users will able to give up-votes, down-votes and comments. I'm storing data in MongoDB. I'm trying to store comments inside it's parent news documents. I'm facing some trouble with views. Look at the my code and below that I explain the problem.
models.py
from djongo import models
# Schema for comments collection which include in belonging news
class Comments(models.Model):
username = models.CharField(max_length=25)
comment = models.TextField()
time_stamp = models.DateTimeField(auto_now_add=True)
class Meta:
abstract = True
# Schema for news collection
class News(models.Model):
username = models.CharField(max_length=25, blank=False)
title = models.CharField(max_length=255, blank=False)
news = models.TextField()
upvotes = models.IntegerField(blank=True)
downvotes = models.IntegerField(blank=True)
time_stamp = models.DateTimeField(auto_now_add=True)
comments = models.EmbeddedModelField(model_container=Comments)
views.py
from rest_framework import generics
from news.api.serializers import NewsSerializer
from news.models import News
class NewsAPIView(generics.CreateAPIView):
lookup_field = 'id'
serializer_class = NewsSerializer
def get_queryset(self):
return News.__objects.all()
serializers.py
from rest_framework import serializers
from news.models import News, Comments
class NewsSerializer(serializers.ModelSerializer):
class Meta:
model = News
fields = '__all__'
What I'm trying to do is that when admin push the news, view should only add username, title and news in database and it should store 0 as default value up-votes and down-votes. Because comments is given by user, view should not expect that when admin push the news. So, how to modify the view so it when news pushed in database it should add username, title, news and time. it should add 0 as default value for up-votes and down-votes and it should not add anything for comments because that document will create when user give some comments.
With this code I'm facing error. So this code is not working.