Artwork

内容由eddyizm and octon提供。所有播客内容(包括剧集、图形和播客描述)均由 eddyizm and octon 或其播客平台合作伙伴直接上传和提供。如果您认为有人在未经您许可的情况下使用您的受版权保护的作品,您可以按照此处概述的流程进行操作https://zh.player.fm/legal
Player FM -播客应用
使用Player FM应用程序离线!

003 - Convert User Creation to REST API endpoint using Django Framework

 
分享
 

Manage episode 192472224 series 1506484
内容由eddyizm and octon提供。所有播客内容(包括剧集、图形和播客描述)均由 eddyizm and octon 或其播客平台合作伙伴直接上传和提供。如果您认为有人在未经您许可的情况下使用您的受版权保护的作品,您可以按照此处概述的流程进行操作https://zh.player.fm/legal
Stream Our Mistakes EP 003
In this episode, we will be working creating a REST API endpoint using Django REST Framework. This will be a multi part series as I plan to use this backend with a Xamarin Android application. No guarantees it is going to work but I will attempt it anyways. This podcast/blog isn't called stream our mistakes for nothing aye?

I looked over a few youtube videos, this blog post and the excellent Django Rest Documentation.
Since this repo is not public, here is the code snippets that are relevant to the video.
Please note: This project assumes you have already implemented Django Rest Framework in your project.
In the Serializers.py file:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 from rest_framework import serializer from django.contrib.auth.models import User class UserCreateSerializer(serializers.ModelSerializer): class Meta: model = User fields = ('first_name', 'last_name', 'email', 'password',) extra_kwargs = {'password': {'write_only': True} } # override create function  def create(self, validated_data): username = validated_data['email'] email = validated_data['email'] first_name = validated_data['first_name'] last_name = validated_data['last_name'] password = validated_data['password'] # I want to set the email as the username so this new object reflects that.  newUser = User( username = email, first_name = first_name, last_name = last_name, email = email, password = password) newUser.set_password(password) newUser.save() #return super().create(validated_data)  return validated_data 

In my views.py:
1 2 3 4
# view  class UserCreateAPIView(CreateAPIView): serializer_class = UserCreateSerializer returnAllUsers = User.objects.all() 

Then we want to include the view in the urls.py:
1 2 3 4
# urls from app.serializers import UserCreateAPIView url(r'^api/register/$', app.views.UserCreateAPIView.as_view()), 

As mentioned above, this video/post does not cover implementing the rest framework from scratch.
My settings file at this point looks like this:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
INSTALLED_APPS = [ # Add your apps here to enable them  'app', 'rest_framework', # default  'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] # currently leaving the permissions open. # in the next episodes, I'll be updating this with token authetication. REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.AllowAny',), } 



Subscribe to the podcast on apple podcasts, google play, stitcher
eddyizm
site: http://eddyizm.com
twitter: http://twitter.com/eddyizm
github: https://github.com/eddyizm
perry
github: https://github.com/apk29
---
**youtube live broadcast:**
https://youtube.com/user/eddyizm/live
Subscribe to our channel and follow my twitter feed to be notified of our next live broadcast and feel free to leave us comments and suggestions on what you want to see.

  continue reading

5集单集

Artwork
icon分享
 
Manage episode 192472224 series 1506484
内容由eddyizm and octon提供。所有播客内容(包括剧集、图形和播客描述)均由 eddyizm and octon 或其播客平台合作伙伴直接上传和提供。如果您认为有人在未经您许可的情况下使用您的受版权保护的作品,您可以按照此处概述的流程进行操作https://zh.player.fm/legal
Stream Our Mistakes EP 003
In this episode, we will be working creating a REST API endpoint using Django REST Framework. This will be a multi part series as I plan to use this backend with a Xamarin Android application. No guarantees it is going to work but I will attempt it anyways. This podcast/blog isn't called stream our mistakes for nothing aye?

I looked over a few youtube videos, this blog post and the excellent Django Rest Documentation.
Since this repo is not public, here is the code snippets that are relevant to the video.
Please note: This project assumes you have already implemented Django Rest Framework in your project.
In the Serializers.py file:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 from rest_framework import serializer from django.contrib.auth.models import User class UserCreateSerializer(serializers.ModelSerializer): class Meta: model = User fields = ('first_name', 'last_name', 'email', 'password',) extra_kwargs = {'password': {'write_only': True} } # override create function  def create(self, validated_data): username = validated_data['email'] email = validated_data['email'] first_name = validated_data['first_name'] last_name = validated_data['last_name'] password = validated_data['password'] # I want to set the email as the username so this new object reflects that.  newUser = User( username = email, first_name = first_name, last_name = last_name, email = email, password = password) newUser.set_password(password) newUser.save() #return super().create(validated_data)  return validated_data 

In my views.py:
1 2 3 4
# view  class UserCreateAPIView(CreateAPIView): serializer_class = UserCreateSerializer returnAllUsers = User.objects.all() 

Then we want to include the view in the urls.py:
1 2 3 4
# urls from app.serializers import UserCreateAPIView url(r'^api/register/$', app.views.UserCreateAPIView.as_view()), 

As mentioned above, this video/post does not cover implementing the rest framework from scratch.
My settings file at this point looks like this:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
INSTALLED_APPS = [ # Add your apps here to enable them  'app', 'rest_framework', # default  'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] # currently leaving the permissions open. # in the next episodes, I'll be updating this with token authetication. REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.AllowAny',), } 



Subscribe to the podcast on apple podcasts, google play, stitcher
eddyizm
site: http://eddyizm.com
twitter: http://twitter.com/eddyizm
github: https://github.com/eddyizm
perry
github: https://github.com/apk29
---
**youtube live broadcast:**
https://youtube.com/user/eddyizm/live
Subscribe to our channel and follow my twitter feed to be notified of our next live broadcast and feel free to leave us comments and suggestions on what you want to see.

  continue reading

5集单集

所有剧集

×
 
Loading …

欢迎使用Player FM

Player FM正在网上搜索高质量的播客,以便您现在享受。它是最好的播客应用程序,适用于安卓、iPhone和网络。注册以跨设备同步订阅。

 

快速参考指南

版权2025 | 隐私政策 | 服务条款 | | 版权
边探索边听这个节目
播放