Help regarding view modification

10 views
Skip to first unread message

Sagar

unread,
Aug 22, 2018, 3:25:20 AM8/22/18
to Django users
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.

Olivier Pons

unread,
Aug 22, 2018, 3:29:56 AM8/22/18
to django...@googlegroups.com
Hi,

You should try this:

upvotes = models.IntegerField(blank=True
, default=0)
downvotes = models.IntegerField(blank=True
, default=0)

(dont forget the makemigrations, then migrate)
tell me if it worked

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8248ce78-78ef-45e6-a091-d6495167630f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Olivier Pons

unread,
Aug 22, 2018, 3:30:26 AM8/22/18
to Django users
Try this:

upvotes = models.IntegerField(blank=True
, default=0)
downvotes = models.IntegerField(blank=True
, default=0)

(dont forget the makemigrations, then migrate)
tell me if it worked

Reply all
Reply to author
Forward
0 new messages