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

Le mercredi 22 août 2018 09:25:20 UTC+2, Sagar a écrit :
>
> 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.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/815c61ba-9be1-40ed-bf6f-00012de5ea73%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to