Django rest_framework oAuth2 APIView error - django

I am making a Django application API, where an authorized user can post a messages, and other users can browse the message and like/dislike/comment on the messages (twitter style).
I have implemented oAuth2 already and tested it.
The API works fine from the admin panel, but when I access it through url, it is giving error:
enter image description here
My models.py class is:
class Posts(models.Model):
def expirationTimeCalculation(self):
EXPIRATION_DURATION = 86400 #time in seconds
expirationTime = self.creationTimestamp + timedelta(seconds = EXPIRATION_DURATION)
return expirationTime
def postLiveStatus(self):
return (self.expirationTimestamp > timezone.now)
def postLiveTimeRemaining(self):
if (self.expirationTimestamp > timezone.now):
return (self.expirationTimestamp - timezone.now).total_seconds()
else:
return 0
postIdentifier = models.IntegerField()
title = models.CharField(max_length=100)
message = models.TextField()
topic = models.ManyToManyField('Topics')
creationTimestamp = models.DateTimeField(default=timezone.now)
expirationTimestamp = property(expirationTimeCalculation)
post_owner = models.ForeignKey(AppUser, related_name='post_owner', on_delete=models.CASCADE)
isLive = property(postLiveStatus)
post_likes = models.ForeignKey('Likes', related_name="post_likes", on_delete=models.CASCADE)
post_dislikes = models.ForeignKey('Dislikes', related_name="post_dislikes", on_delete=models.CASCADE)
post_comments = models.ForeignKey('Comments', related_name="post_comments", on_delete=models.CASCADE)
def __str__(self):
return self.title
class Topics(models.Model):
TOPICS = [('P','Politics'),('H','Health'),('S','Sports'),('T','Tech')]
topicName = models.CharField(max_length=2, choices=TOPICS, blank=False)
class Likes(models.Model):
isLiked = models.BooleanField(null=True)
likes_owner = models.ForeignKey(AppUser, related_name='likes_owner', on_delete=models.CASCADE)
post = models.ForeignKey(Posts, related_name='likes', on_delete = models.CASCADE)
class Dislikes(models.Model):
isDisliked = models.BooleanField(null=True)
dislikes_owner = models.ForeignKey(AppUser, related_name='dislikes_owner', on_delete=models.CASCADE)
post = models.ForeignKey(Posts, related_name='dislikes', on_delete = models.CASCADE)
class Comments(models.Model):
comment = models.TextField(null=True)
comments_owner = models.ForeignKey(AppUser, related_name='comments_owner', on_delete=models.CASCADE)
post = models.ForeignKey(Posts, related_name='comments', on_delete = models.CASCADE)
The serializer class is:
class CommentsSerializer(serializers.ModelSerializer):
comments_owner = CreateUserSerializer()
class Meta:
model = Comments
fields = ('comment', 'comments_owner')
class DislikesSerializer(serializers.ModelSerializer):
dislikes_owner = CreateUserSerializer()
class Meta:
model = Dislikes
fields = ('isDisliked', 'dislikes_owner')
class LikesSerializer(serializers.ModelSerializer):
likes_owner = CreateUserSerializer()
class Meta:
model = Likes
fields = ('isLiked', 'likes_owner')
class TopicsSerializer(serializers.ModelSerializer):
class Meta:
model = Topics
fields = ('topicName')
class PostsSerializer(serializers.ModelSerializer):
class Meta:
model = Posts
fields = ('postIdentifier', 'title', 'message', 'topic', 'creationTimestamp', 'expirationTimestamp', 'isLive', 'post_owner', 'post_likes', 'post_dislikes', 'post_comments')
Views class is:
from rest_framework.generics import ListAPIView
from .models import Posts
from .serializers import PostsSerializer
class PostsView(ListAPIView):
queryset = Posts.objects.all()
serilaizer_class = PostsSerializer
And the main URLS class is:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
import posts_backend.views
urlpatterns = [
path('admin/', admin.site.urls),
path('o/', include('oauth2_provider.urls', namespace='oauth2_provider')), #Adding oAuth2 privider URLS to the url path
path('authentication/', include('accounts.urls')), #Creating authentication endpoints http://10.61.64.150:8000/authentication/ for performing authentication
path('v1/posts/', posts_backend.views.PostsView.as_view(),), # Version 1 of the API
]
Can anyone kindly let me know why I am getting this error and how to resolve this?

try renaming the attribute from serilaizer_class to serializer_class

Related

Mutation in django graphene for model with foreign key and many to many relationship

I have 2 models in my django app, the first is Tags model and the second is Post model, the problem is when i try to use mutation for the Post model to add a post from the graphql it doesn't work but it works fine in the Tags model, also the Queries works fine when i try to get data from the database.
Here's my Code:
model.py:
from django.db import models
from django.utils.translation import gettext as _
from django.conf import settings
from django.contrib.auth import get_user_model
User = get_user_model()
# Create your models here.
class Tag(models.Model):
name = models.CharField(_("Tag Name"), max_length=50, unique=True)
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(_("Title"), max_length=50, unique=True)
slug = models.SlugField(_("Post Slug"), unique=True)
body = models.TextField(_("Post Content"))
createdAt = models.DateTimeField(auto_now_add=True)
updatedAt = models.DateTimeField(auto_now=True)
published = models.BooleanField(_("Published"), default=False)
author = models.ForeignKey(User, verbose_name=_("Author"), on_delete=models.CASCADE)
tags = models.ManyToManyField("Tag", verbose_name=_("Tags"), blank=True)
class Meta:
ordering = ['-createdAt']
def __str__(self):
return self.title
schema.py:
import graphene
from graphene_django import DjangoObjectType
from django.contrib.auth import get_user_model
from .models import Post, Tag
User = get_user_model()
class PostType(DjangoObjectType):
class Meta:
model = Post
class TagType(DjangoObjectType):
class Meta:
model = Tag
fields = ('id', 'name',)
class TagInput(graphene.InputObjectType):
name = graphene.String(required=True)
class CreatePostMutation(graphene.Mutation):
class Arguments:
title = graphene.String()
body = graphene.String()
published = graphene.Boolean()
author_id = graphene.ID()
tags = graphene.List(graphene.Int)
success = graphene.Boolean()
post = graphene.Field(PostType)
#classmethod
def mutate(cls, root, info, title, body, published, author_id, tags):
author = User.objects.get(pk=author_id)
post_instance = Post(
title=title,
body=body,
published=published,
author=author,
)
post_instance.save(commit=False)
if tags:
tag_objects = Tag.objects.filter(pk__in=tags)
post_instance.tags.set(tag_objects)
post_instance.save()
return CreatePostMutation(success=True, post=post_instance)
class CreateTagMutation(graphene.Mutation):
class Arguments:
name = graphene.String(required=True)
tag = graphene.Field(TagType)
#classmethod
def mutate(cls, root, info, name):
tag = Tag(name=name)
tag.save()
return CreateTagMutation(tag=tag)
class Mutation(graphene.ObjectType):
create_post = CreatePostMutation.Field()
create_tag = CreateTagMutation.Field()
schema = graphene.Schema(query=Query, mutation=Mutation)
Here's what it returns:
{
"data": {
"createPost": {
"post": null
}
}
}

NOT NULL constraint failed: shipping_ship.user_id Django

So I'm working on a shipping website with the django rest framework. The website brings two to four people together so they can easily ship their goods together at the same time. But I'm facing a major stumbling block on the views where user book a shipping the code is below.
models.py
from django.db import models
from django.contrib import get_user_model
User = get_user_model()
class Container(models.Model):
container_type = models.Charfield(max_length = 30, blank=False, null = False)
max_users = models.IntegerField()
price = models.DecimalField(max_digits=10, decimal_places =2, default=0, blank=True, null=True)
users = models.ManyToManyField(User)
class Ship(models.Model):
container = models.ForeignKey(Container, related_name='cont', on_delete=models.CASCADE)
user = models.ForeignKey(User, related_name='shipper', on_delete=models.CASCADE)
location = (
('France', 'France'),
)
from_location = models.CharField(max_length=30, choices=location, blank=False, null=False)
to_location = (
('Lagos', 'Lagos'),
('Abuja', 'Abuja'),
('Abeokuta', 'Abeokuta'),
('Osun', 'Osun'),
)
to_location = models.CharField(max_length=30, choices=to_location, blank=False, null=False)
date_leaving = models.DateField(auto_now=False)
price = models.DecimalField(max_digits=10, decimal_places=2, default=0, blank=True, null=True)
def __str__(self):
return self.user
then my serializer.py file
from rest_framework import serializers
from .models import Container, Ship
class ContainerSerializer(serializers.ModelSerializer):
class Meta:
model = Container
fields = '__all__'
class MiniContainerSerializer(serializers.ModelSerializer):
class Meta:
model = Container
fields =['container_type', 'price']
class ShipSerializer(serializers.ModelSerializer):
class Meta:
model = Ship
fields = '__all__'
read_only_fields = ('user', 'price')
class MiniShipSerializer(serializers.ModelSerializer):
class Meta:
model = Ship
fields = ['container', 'from_location', 'to_location']
and now my views.py file which I have issues with
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from rest_framework.generics import ListCreateAPIView, CreateAPIView, ListAPIView, RetrieveUpdateDestroyAPIView, RetrieveAPIView
from .serializers import ContainerSerializer, MiniContainerSerializer, ShipSerializer, MiniShipSerializer
from rest_framework import permissions, status
from rest_framework.response import Response
from .models import Container, Ship
class ShipAPI(ListCreateAPIView):
serializer_class = ShipSerializer
def get_queryset(self):
user = self.request.user
queryset = Ship.objects.filter(user=user)
return queryset
def Book_shipping(self, request, *args, **kwargs):
user = request.user
container = get_object_or_404(Container, pk=request.data['container'])
if container.users.count() >= container.max_users:
return Response('container already full')# here i'm trying to set limits so the users joining each container won't surpass the max users.
cont = container(users=user)
cont.save()
from_location = (request.data['from_location'])
to_location = (request.data['to_location'])
date_leaving = int(request.data['date_leaving'])
price = container.price / container.max_users
cart = Ship(container=container, user=user, from_location=from_location, to_location=to_location, date_leaving=date_leaving, price=price)
cart.save()
serializer = ShipSerializer(cart)
data ={'message': 'shipping successfully created',
'data':serializer.data}
return Response(data=data, status=status.HTTP_201_CREATED)
and then after testing the endpoint it returns this error:
IntegrityError at /Shipping/Ship/
NOT NULL constraint failed: shipping_ship.user_id
I've tried debugging and looking at it over and over again can someone please help me? Thanks in advance. And yes I've tried deleting migrations and the database.
As your Container model have a ManyToMany relationship with the User model.
So it may not work like cont = container(users=user)
For me it worked like this:
cont = container.users.add(user)
cont.save()

How to write query with in a model with foreign key relation?

class Categorie(models.Model):
name = models.CharField(max_length=200)
description = models.TextField(max_length=2000)
status = models.BooleanField(default=True)
added_date = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return u'%s' % self.name
class GroupQuestion(models.Model):
category = models.ForeignKey(Categorie, related_name='%(class)s_name_related', null=True)
name = models.CharField(max_length=200)
added_date = models.DateTimeField(auto_now_add=True)
status = models.BooleanField(default=True)
class Meta:
verbose_name = 'Group of Questions'
verbose_name_plural = 'Group of Questions'
def __unicode__(self):
return u'%s' % self.name
class Question(models.Model):
category = models.ForeignKey(Categorie, related_name='%(class)s_name_related', null=True)
group = models.ForeignKey(GroupQuestion, related_name='%(class)s_name_related')
name = models.TextField(max_length=200)
added_date = models.DateTimeField(auto_now_add=True)
status = models.BooleanField(default=True)
#select_choice = models.CharField(max_length=5, choices=QUESTION_CHOICES, null=True)
positive_answer = models.PositiveIntegerField()
negative_answer = models.PositiveIntegerField()
def __unicode__(self):
return u'%s' % self.name
I have these three model. When I pass category_id from my API wants to get QuestionGroups and Questions related to that group?
Some thing like this
"data": [
{
id:1
name = ABC
"question":[
{
"id":1
"name":ABC
}
{
"id":1
"name":ABC
}
]
}
{
id:1
name = ABC
"question":[
{
"id":1
"name":ABC
}
{
"id":1
"name":ABC
}
]
}
}
I am new in django some can help me how to write query in view and how to serialize data.
You are looking for a nested serialization.
You should create two serializers. One for your GroupQuestion model and one for your Question model. You will use in your QuestionSerializer inside your GroupQuestionSerializer in order to perform a nested serialization.
In your serializers.py:
from rest_framework import serializers
from .models import GroupQuestion, Question
# Serializer for the Question model
class QuestionSerializer(serializers.ModelSerializer):
class Meta:
model = Question
fields = ('id', 'name')
# Serializer for the GroupQuestion model
class GroupQuestionSerializer(serializers.ModelSerializer):
question_name_related = QuestionSerializer(read_only=True, many=True)
# The magic happens here ^
# You use your QuestionSerializer inside your GroupQuestionSerializer
# In order to serialize the related Questions for this GroupQuestion
class Meta:
model = GroupQuestion
fields = ('id', 'question_name_related')
Then let's say you have a simple ListAPIView for listing your GroupQuestion entries.
In your views.py:
from rest_framework import generics
from .models import GroupQuestion
from .serializers import GroupQuestionSerializer
# Import the serializer that we have just created
class GroupQuestionList(generics.ListAPIView):
serializer_class = GroupQuestionSerializer
def get_queryset(self):
category_id = self.kwargs['category_id']
category = generics.get_object_or_404(Categorie, id=category_id)
return GroupQuestion.objects.filter(category=category)
This view will simply serialize all your GroupQuestion models, which have related Categorie with the given id.
Finally, map this view in your urls and when you access this endpoint, you should get the expected result.
In your urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^groups/(?P<category_id>[0-9]+)/$', views.GroupQuestionList.as_view(), name='group_question_list')
]
Note: I am not sure that question_name_related is the valid related_name for the Question model in your GroupQuestion.

Nested router not working

I'm new to Django and am struggling to get nested routers working. Basically, I'm trying to model the endpoint /api/v1/organizers/1/events/1, where I have Event and Organizer models as follows:
class Event(models.Model):
class Meta:
db_table = 'events'
STATUS_CHOICES = (
('scheduled', 'Scheduled'),
('completed', 'Completed'),
('cancelled', 'Cancelled')
)
name = models.TextField()
purpose = models.TextField()
date_time = models.DateTimeField()
description = models.TextField()
programme = models.TextField()
entry_fee = models.DecimalField(max_digits=6, decimal_places=2)
status = models.TextField(choices=STATUS_CHOICES)
comments = models.TextField(null=True)
people_invited = models.ManyToManyField('Contact', through='EventInvitation')
organizer = models.ForeignKey('Organizer', on_delete=models.CASCADE)
class Organizer(models.Model):
STATUS_CHOICES = (
('inactive', 'Inactive'),
('active', 'Active'),
)
class Meta:
db_table = 'organizers'
first_name = models.TextField()
middle_name = models.TextField(null=True)
last_name = models.TextField(null=True)
email = models.OneToOneField('Email', on_delete=models.CASCADE)
company_name = models.TextField(null=True)
company_description = models.TextField(null=True)
password = models.TextField()
login_token = models.TextField(null=True)
registered_on = models.DateTimeField(null=True)
status = models.TextField(choices = STATUS_CHOICES, default='inactive')
I created another app called rest_api to handle the API. The models are stored in an app called shared_stuff. Anyway, here's the project-level urls.py (don't mind the front_end app for now):
from django.conf.urls import include, url
urlpatterns = [
url(r'^api/v1/', include('rest_api.urls')),
url(r'^', include('frontend.urls')),
]
And here's the urls.py from the app rest_api:
from django.conf.urls import url, include
from rest_framework_nested import routers
from .views import *
router = routers.SimpleRouter()
# /organizers/12/events/1
router.register(r'organizers', OrganizerViewSet, base_name='organizers')
organizer_router = routers.NestedSimpleRouter(router, r'organizers', lookup='organizers')
organizer_router.register(r'events', EventViewSet, base_name='organizers-events')
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^', include(organizer_router.urls)),
]
Here's the serializers.py for rest_api app:
class EventSerializer(serializers.ModelSerializer):
class Meta:
model = Event
fields = ['id', 'name', 'purpose', 'date_time', 'description', 'programme', 'entry_fee', 'status', 'comments']
class OrganizerSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Organizer
fields = ['id', 'first_name', 'middle_name', 'last_name', 'email', 'company_name', 'company_description', 'registered_on', 'status']
events = serializers.HyperlinkedIdentityField(
view_name = 'events_list',
lookup_url_kwarg = 'organizer_pk'
)
And finally, here's the views.py from the app rest_api:
from rest_framework import viewsets
from .models import *
from .serializers import *
class EventViewSet(viewsets.ModelViewSet):
def list(self, request, organizer_pk=None, name='events_list'):
events = self.queryset.filter(organizer=organizer_pk)
serializer = EventSerializer(events, many=True)
return Response(serializer.data)
class OrganizerViewSet(viewsets.ModelViewSet):
def list(self, request, name='organizers_list'):
data = Organizer.objects.all()
serializer = OrganizerSerializer(data, many=True)
return Response(serializer.data)
I'm sure there are many things broken in my code, and that's where I need help. The problem is I'm getting the following error:
TypeError: list() got an unexpected keyword argument 'organizers_pk'
I'm not sure what's wrong, and will appreciate some help!
I got it working by changing the EventViewSet to the following:
def list(self, request, organizers_pk=None, name='events_list'):
events = self.queryset.filter(organizer=organizers_pk)
serializer = EventSerializer(events, many=True)
return Response(serializer.data)
I'm not sure why, but the expected keyword argument name is organizers_pk, whereas I had organizer_pk. I would like to know why this is so, but other than that my problem is solved for now.

django not add auto increment primary key id when save an article

I hava an article app installed in django admin site,when i finish editing one article,I click the save button,but an error page:
article/models.py
# blog category models
class Category(models.Model):
id = models.IntegerField(primary_key=True,help_text='primary key')
name = models.CharField(max_length=50,help_text='category name')
description = models.TextField(default='',help_text='category description')
createtime = models.DateTimeField(auto_now_add=True)
modifytime = models.DateTimeField(auto_now=True)
categories = models.Manager()
class Meta:
db_table = 'article_category'
def __str__(self):
return self.name
#blog article models
class Article(models.Model):
STATUS = (
(0,'on'),
(1,'off')
)
id = models.IntegerField(primary_key=True,help_text='primary key')
category = models.ForeignKey(Category,help_text='foreigner key reference Category')
title = models.CharField(max_length=100,help_text='article title')
content = models.TextField(help_text='article content')
like = models.IntegerField(default=0,help_text='like numbers')
secretcode = models.CharField(max_length=512,help_text='who has the code can scan')
status = models.IntegerField(choices=STATUS,help_text='status of the article')
createtime = models.DateTimeField(auto_now_add=True,help_text='time that first created')
modifytime = models.DateTimeField(auto_now=True,help_text='time when modified')
articles = models.Manager()
class Meta:
db_table = 'article'
article/widgets.py
from pagedown.widgets import AdminPagedownWidget
from django import forms
from .models import Article
class ArticleModelForm(forms.ModelForm):
content = forms.CharField(widget=AdminPagedownWidget())
class Meta:
model = Article
fields = ('title','category', 'content', 'secretcode', 'status')
article/admin.py
from django.contrib import admin
from .widgets import ArticleModelForm
from .models import Article,ArticleImage,Category
class MMBArticleAdmin(admin.ModelAdmin):
form = ArticleModelForm
admin.site.register(Article,MMBArticleAdmin)
admin.site.register(Category)
admin.site.register(ArticleImage)
the page in the admin site looks likeļ¼š
and then I click save ,the error page show up like above!why did this happen?and how to fix it?
You've overridden the default automatic field with a manual non-autoincrementing ID. Don't do that. Remove your id fields altogether.