can anyone help please, with DRF
according to POST request, I want to create(if not exists) or update() table
belows are my codes
model.py
class User1(models.Model):
user = models.CharField(max_length=10)
full_name = models.CharField(max_length=20)
logon_data = models.DateTimeField(blank=True, null=True)
serializer.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User1
fields = '__all__'
views.py
from .models import User1
from .serializers import UserSerializer
from rest_framework.response import Response
from rest_framework.decorators import api_view
#api_view(['GET', 'POST'])
def UserView(request):
if request.method == 'GET':
users = User1.objects.all()
serializer = UserSerializer(users, many=True)
return Response(serializer.data)
elif request.method == 'POST':
users = User1.objects.all()
serializer = UserSerializer(data=request.data, many=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('users/', views.UserView),
]
when POST request, I want to check like this:
if user exists:
if user.full_name == request.fullname
update (user.logon_data)
save()
else:
update (user.full_name)
update (user.logon_data)
save()
else:
create(
user = request.user,
full_name = request.full_name,
logon_data = request.logon_date)
save()
POST request for JSON like this:
[
{
"user": "testuser1",
"full_name": "test user1",
"logon_data": "2022-10-19 09:37:26"
},
{
"user": "testuser2",
"full_name": "test user2",
"logon_data": "2022-10-20 07:02:06"
}
]
#api_view(['GET', 'POST'])
def UserView(request):
if request.method == 'GET':
users = User1.objects.all()
serializer = UserSerializer(users, many=True)
return Response(serializer.data)
elif request.method == 'POST':
serializer = UserSerializer(data=request.data, many=True)
# if serializer validation fails, raises error by itself
serializer.is_valid(raise_exception=True)
for data in serializer.validated_data:
# checking if data exists else creating an object in User1 model
# user = data['user'] --> filter to check if that user exist
# defaults={'full_name': data['full_name'], 'logon_data': #data['logon_data']} --> value provided in defaults is used to update data in #model once the condition is met.
User1.objects.update_or_create(user=data['user'], defaults={'full_name': data['full_name'], 'logon_data': data['logon_data']})
return Response(serializer.data, status=201)
Try this update_or_create it will check based on request.user whether it exists in table or not
Also ensure that request.user is checked to user object if you want to check specific field then it should be request.user.user
User.objects.update_or_create(user=request.user.user, defaults={"full_name": request.full_name, "logon_date":request.logon_date})
Related
I have POST method in views.py in django to create an entry in database
I can create a single entry using postman, but can not create bulk entries using postman
can anyone help, please?
models.py file
from django.db import models
class Users(models.Model):
user = models.CharField(max_length=50,default='')
function = models.CharField(max_length=50,default='')
department = models.CharField(max_length=50,default='')
logon = models.CharField(max_length=50,default='')
def __str__(self):
return self.user+" - Last_Logon: "+self.logon
class Meta:
ordering = ('id',)
serializers.py file
from rest_framework import serializers
from activities.models import Users
class UsersSerializer(serializers.ModelSerializer):
class Meta:
model = Users
fields = ('id', 'user', 'function', 'department', 'logon')
views.py file with GET,POST,PUT,DELETE, from this file POST method creates only a single entry
from django.shortcuts import render
from django.http.response import JsonResponse
from rest_framework.parsers import JSONParser
from rest_framework import status
from activities.models import Users
from activities.serializers import UsersSerializer
from rest_framework.decorators import api_view
#api_view(['GET', 'POST'])
def users_list(request):
if request.method == 'GET':
users = Users.objects.all()
user = request.GET.get('user', None)
if user is not None:
users = users.filter(user__icontains=user)
users_serializer = UsersSerializer(users, many=True)
return JsonResponse(users_serializer.data, safe=False)
# 'safe=False' for objects serialization
elif request.method == 'POST':
users_data = JSONParser().parse(request)
users_serializer = UsersSerializer(data=users_data)
if users_serializer.is_valid():
users_serializer.save()
return JsonResponse(users_serializer.data, status=status.HTTP_201_CREATED)
return JsonResponse(users_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#api_view(['GET', 'PUT', 'DELETE'])
def users_detail(request, pk):
try:
users = Users.objects.get(pk=pk)
except Users.DoesNotExist:
return JsonResponse({'message': 'The user does not exist'}, status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
users_serializer = UsersSerializer(users)
return JsonResponse(users_serializer.data)
elif request.method == 'PUT':
users_data = JSONParser().parse(request)
users_serializer = UsersSerializer(users, data=users_data)
if users_serializer.is_valid():
users_serializer.save()
return JsonResponse(users_serializer.data)
return JsonResponse(users_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE':
users.delete()
return JsonResponse({'message': 'User was deleted successfully!'}, status=status.HTTP_204_NO_CONTENT)
urls.py file
from django.urls import re_path
from activities import views
urlpatterns = [
re_path(r'^api/activities$', views.users_list),
re_path(r'^api/activities/(?P<pk>[0-9]+)$', views.users_detail),
]
if I set many=True in user serializer, UsersSerializer(data=users_data, many=True) for POST metho
In postman, for sending a single entry, got this error
for sending multiple entries, got this error
for UsersSerializer(data=users_data, many=True) you must send list of json objects
[
{
"User": "user1",
"function": "t",
"deportment": "t",
"logon": "23...."
},
{
"User": "user1",
"function": "t",
"deportment": "t",
"logon": "23...."
}
]
and for a user UsersSerializer(data=users_data)
you can split serializers with a send a query param into your url and in view function
if request.GET.get('bulk'):
serializer = UsersSerializer(data=users_data, many=True)
else:
serializer = UsersSerializer(data=users_data)
I am using Postman to get my request. My GET and POST appear to be working fine. It's only when I go to update the data with PUT that its where I am running into the hiccup. Postman actually sends data back as if the object is being updated, but when I go to check via GET it's the same data as before. I have tried adding the hive data to the serializer.save, but it tells me I'm adding too many parameters.
models.py
class Inspection(models.Model):
hive = models.ForeignKey(Hive, on_delete=models.CASCADE)
user = models.ForeignKey(User,on_delete=models.CASCADE)
eggs = models.IntegerField()
larvae = models.IntegerField()
sealed_brood = models.IntegerField()
covered_bees = models.IntegerField()
nectar_honey = models.IntegerField()
pollen = models.IntegerField()
pest_spotted = models.CharField(max_length=200)
pest_action = models.CharField(max_length=200)
notes_concerns = models.CharField(max_length=300)
serializers.py
class InspectionSerializer(serializers.ModelSerializer):
class Meta:
model = Inspection
fields = ['id', 'eggs', 'larvae', 'sealed_brood', 'covered_bees', 'nectar_honey', 'nectar_honey', 'pollen', 'pest_spotted', 'pest_action', 'notes_concerns','user_id','hive','hive_id']
depth = 1
hive_id = serializers.IntegerField(write_only=True)
Views.py
#api_view(['GET', 'POST','PUT'])
#permission_classes([IsAuthenticated])
def inspection_details(request, pk):
hive = get_object_or_404(Hive, pk=pk)
inspection = Inspection.objects.filter(hive_id = hive.id, user=request.user)
if request.method == "GET":
serializer = InspectionSerializer(inspection, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
elif request.method == 'POST':
serializer = InspectionSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save(user=request.user)
return Response(serializer.data,status.HTTP_200_OK)
elif request.method == 'PUT':
serializer = InspectionSerializer(hive, data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save(user=request.user)
return Response(serializer.data, status=status.HTTP_200_OK)
I think the instance variable is not correct, it should be inspection not hive.
#api_view(['GET', 'POST', 'PUT'])
#permission_classes([IsAuthenticated])
def inspection_details(request, pk):
hive = get_object_or_404(Hive, pk=pk)
inspection = Inspection.objects.filter(hive_id = hive.id, user=request.user)
...
elif request.method == 'PUT':
serializer = InspectionSerializer(inspection, data=request.data) # here
if serializer.is_valid(raise_exception=True):
serializer.save(user=request.user)
return Response(serializer.data, status=status.HTTP_200_OK)
First of all don't use if clause for is_valid. it raise exception anyway so if is not needed.
Next you need to update your serializer not saving it.
elif request.method == 'PUT':
serializer = InspectionSerializer(hive, data=request.data)
serializer.is_valid(raise_exception=True)
serializer.update(object_to_update,serializer.validated_data)
# if you want to return updated serialized data :
updated_serializer = InspectionSerializer(object_to_update)
return Response(updated_serializer.data, status=status.HTTP_200_OK)
I have a API view in place where I first want to create a new user (already working) and second I want to return the new created user object using my UserSerializer (Not working).
views.py
#api_view(['POST'])
#permission_classes([AllowAny])
def user_create(request):
exception_handler = UserUnavailable
success_handler = UserCreated
if request.method == 'POST':
creation_serializer = CreateUserSerializer(data=request.data)
try:
if creation_serializer.is_valid(raise_exception=True):
creation_serializer.save()
user_serializer = UserSerializer(data=creation_serializer.instance.id)
if user_serializer.is_valid():
return JsonResponse({"status_code": success_handler.status_code,
"default_detail": success_handler.default_detail,
"default_code": success_handler.default_code,
"new_user": user_serializer,
}, safe=False)
except APIException:
return JsonResponse({"status_code": exception_handler.status_code,
"default_detail": exception_handler.default_detail,
"default_code": exception_handler.default_code
}, safe=False)
I can confirm that creation_serializer.instance.id contains the new users id.
serializers.py
class UserSerializer(serializers.ModelSerializer):
id = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
class Meta:
model = get_user_model()
fields = ('id', 'user')
read_only_fields = ('id', 'user')
I can also confirm that
if user_serializer.is_valid()
does not validate for some reason. Any ideas
Thanks in advance
To initialize a serializer with user instance instead of
user_serializer = UserSerializer(data=creation_serializer.instance.id)
You should write something like this:
user_serializer = UserSerializer(instance=creation_serializer.instance)
instead of
Also, you don't have to validate the object that has already been saved.
And create a response you should like this:
return JsonResponse({
"status_code": success_handler.status_code,
"default_detail": success_handler.default_detail,
"default_code": success_handler.default_code,
"new_user": user_serializer.data,
}, safe=False)
But using api_view is now a bad thing. It's much better to use ModelViewSet. You can use something like this:
class UserViewSet(ModelViewSet):
serializer_class = UserSerializer
queryset = User.objects.all()
def get_serializer_class(self, *args, **kwargs):
if self.action == 'create':
return CreateUserSerializer
return super().get_serializer_class(*args, **kwargs)
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
response_serializer = self.serializer_class(instance=serializer.instance)
return Response(response_serializer.data, status=status.HTTP_201_CREATED, headers=headers)
And I'm sorry, I haven't tested it, there might be typos.
You can read about ModelViewsets more here https://www.django-rest-framework.org/api-guide/viewsets/#modelviewset
In creating Django REST framework, i'll get all the data using this code
views.py
#api_view(['GET', ])
def api_detail_educationlevel(request):
if request.method == 'GET':
educationlevel = EducationLevel.objects.all()
serializer = EducationLevelSerializer(educationlevel, many=True)
return Response(serializer.data)
urls.py
path('api/', views.api_detail_educationlevel),
but when i add in my views.py like this
#api_view(['PUT', ])
def api_update_educationlevel(request, pk):
try:
educationlevel = EducationLevel.objects.get(pk=pk)
except EducationLevel.DoesNotExist:
return HttpResponse(status=404)
if request.method == 'PUT':
serializer = EducationLevelSerializer(educationlevel, data=request.data)
data = {}
if serializer.is_valid():
serializer.save()
data["success"] = "update successfull"
return Response(data=data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#api_view(['DELETE', ])
def api_delete_educationlevel(request, pk):
try:
educationlevel = EducationLevel.objects.get(pk=pk)
except EducationLevel.DoesNotExist:
return HttpResponse(status=404)
if request.method == 'DELETE':
operation = educationlevel.delete()
data ={}
if operation:
data["success"] = "delete successfull"
else:
data["failure"] = "delete failed"
return Response(data=data)
#api_view(['POST', ])
def api_create_blog_view(request):
authentication_classes = [SessionAuthentication, BasicAuthentication]
permission_classes = [IsAuthenticated]
def get(self, request, format=None):
content = {
'user': unicode(request.user), # `django.contrib.auth.User` instance.
'auth': unicode(request.auth), # None
}
return Response(content)
and in my urls.py
urlpatterns = [
path('api/', views.api_detail_educationlevel),
path('api/update/', views.api_update_educationlevel),
path('api/delete/', views.api_delete_educationlevel),
path('api/create/', views.api_create_blog_view),
]
urlpatterns = format_suffix_patterns(urlpatterns)
I dont know why i am getting this message,
in my path('api/update/', views.api_update_educationlevel),
in my path('api/delete/', views.api_delete_educationlevel),
in my path('api/create/', views.api_create_blog_view),
any idea why i am getting this message? i just want functional rest framework that can update/delete/insert data using user auth account
For each of these views (views.api_detail_educationlevel, views.api_update_educationlevel, views.api_delete_educationlevel, views.api_create_blog_view), you define #api_view(['*api method*', ]).
Only the views.api_detail_educationlevel has #api_view(['GET', ]) therefore allowing a GET method. The others don't. Either add a GET method to the other views or, like the documentation you follow, create a class containing each method.
I'm trying to learn DJango Rest so I made a litte test to see if I could obtain some things from the database, but I'm getting some problems.
Here's my models.py:
from django.db import models
# Create your models here.
class Stock(models.Model):
ticker = models.CharField(max_length=10)
open = models.FloatField()
close = models.FloatField()
volume = models.IntegerField()
def __str__(self):
return self.ticker
Here's my serializers.py:
from rest_framework import serializers
from .models import Stock
class StockSerializer(serializers.ModelSerializer):
ticker = serializers.CharField()
open = serializers.FloatField()
close = serializers.FloatField()
volume = serializers.IntegerField()
def create(self, validated_data):
return Stock.objects.check(**validated_data)
def update(self, instance, validated_data):
instance.ticker = validated_data.get('ticker', instance.ticket)
instance.open = validated_data.get('open', instance.open)
instance.close = validated_data.get('close', instance.close)
instance.volume = validated_data.get('volume', instance.volume)
instance.save()
return instance
class Meta:
model = Stock
fields = '__all__'
Here's my views.py:
from django.http import Http404
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Stock
from .serializers import StockSerializer
# List all stocks or create a new one
# stocks/
#api_view(['GET', 'POST'])
def stock_list(request, format=None):
if request.method == 'GET':
stocks = Stock.objects.all()
serializer = StockSerializer(stocks, many=True)
return Response(serializer.data)
elif request.method == 'POST':
serializer = StockSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#api_view(['GET', 'POST', 'DELETE'])
def stock_detail(request, pk, format=None):
try:
stock = Stock.objects.get(pk=pk)
except Stock.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
serializer = StockSerializer(stock)
return Response(serializer.data)
elif request.method == 'PUT':
serializer = StockSerializer(stock, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE':
stock.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
And finally, here's my url.py:
from django.conf.urls import url
from django.contrib import admin
from rest_framework.urlpatterns import format_suffix_patterns
from companies import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^stocks/', views.stock_list),
url(r'^stocks/(?P<pk>[0-9]+)$', views.stock_detail),
]
urlpatterns = format_suffix_patterns(urlpatterns)
I've been following this tutorial, but when it comes to the moment of making some requests (for this example I use this one: http http://127.0.0.1:8000/stocks/
I get this error message:
TypeError at /stocks/ stock_list() missing 1 required positional
argument: 'request'
I think that the problem is with the urls, but I'm not sure how to fix it.
Any ideas and some examples?
UPDATE: The problem was with the methods in view (they had an attibute self)
The general Get method works, but when I try to use POST
POST ERROR:
When I try this request: http --form POST http://127.0.0.1:8000/stocks/ ticker='SAM'
I get this error:
AttributeError at /stocks/ Got AttributeError when attempting to get a
value for field ticker on serializer StockSerializer. The
serializer field might be named incorrectly and not match any
attribute or key on the list instance. Original exception text was:
'list' object has no attribute 'ticker'.
You need to remove self.
Remember you are using functions not clases.
#api_view(['GET', 'POST'])
def stock_list(request, format=None):
if request.method == 'GET':
stocks = Stock.objects.all()
serializer = StockSerializer(stocks, many=True)
return Response(serializer.data)
elif request.method == 'POST':
serializer = StockSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Try views.stock_list.as_view() in urls.py