day075作业

from django.conf.urls import url
from django.views.static import serve
from project01.settings import MEDIA_ROOT 
from . import views 


urlpatterns = [
    url(r'^media/(?P<path>.*)$', serve, {"document_root", MEDIA_ROOT}),
    url(r'^users/$', views.UserAPIView.as_view()),
    url(r'^users/(?P<pk>\d+)', views.UserAPIView.as_view()),
    url(r'^v2/users/$',  views.UserReadAPIView.as_view()),

    url(r'^v3/users/$',  views.UserModelAPIView.as_view({
        'get': 'list',
        'post': 'create',
    })),
    url(r'^v3/users/(?P<pk>\d+)$',  views.UserModelAPIView.as_view({
        'get': 'retrieve',
        'put': 'update',
        'patch': 'partial_update',
        'delete': 'destroy',
    })),
]
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from . import serializers
from . import models

# Create your views here.



from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.viewsets import ModelViewSet

class UserReadAPIView(ListAPIView):
    queryset = models.User.objects.all()
    serializer_class = serializers.UserSerializer

    def get(self, request):
        return self.list(request)


class UserModelAPIView(ModelViewSet):
    queryset = models.User.objects.all()
    serializer_class = serializers.UserSerializer
    lookup_url_kwarg = 'pk'
    
    def destroy(self, request, *args, **kwargs):
        user_obj = self.get_object()
        user_obj.is_delete = 1
        user_obj.save()
        return Response({'msg': "删除成功"}, status=200)

from rest_framework import serializers
from . import models


class UserSerializer(serializers.ModelSerializer):
    re_password = serializers.CharField(write_only=True, required=True)
    class Meta:
        model = models.User
        fields = ['username', 'password', 'avatar', 'gender', 'avatar', 'show_gender', 're_password', 'get_avatar']
        extra_kwargs = {
            'avatar': {
                'write_only': True,
            },
            'password': {
                'write_only': True,
                'required': True,
            },
            'gender': {
                'write_only': True,
            },
        }

    def validate(self, attrs):
        password = attrs.get('password')
        try:
            re_password = attrs.pop('re_password')
            if password != re_password:
                raise serializers.ValidationError({'re_password': '两次密码输入不一致'})
            return attrs
        except:
            return attrs
from django.db import models
from project01 import settings

# Create your models here.


class User(models.Model):
    username = models.CharField(max_length=255)
    password = models.CharField(max_length=255)
    avatar = models.ImageField(upload_to='img', default='img/default')
    GENDER_CHOICE = ([(0, 'male'), (1, 'female')])
    gender = models.IntegerField(choices=GENDER_CHOICE, default=0)
    is_delete = models.SmallIntegerField(default=0)

    @property
    def show_gender(self):
        return self.get_gender_display()
    
    @property
    def get_avatar(self):
        return '%s%s%s' % (settings.BASE_URL, settings.MEDIA_URL, self.avatar)
posted @ 2019-12-30 20:15  YajunRan  阅读(120)  评论(0编辑  收藏  举报