day 76作业

作业

1.完成汽车系统表模型的建立(表关系自己设计)
    汽车表、汽车商家表、赞助商表、赞助商详情表
# models

from django.db import models

# Create your models here.
class Car(models.Model):
    COLOUR_CHOICES=(
        (0,'银色'),
        (1,'黑色'),
        (2,'红色'),
        (3,'灰色'),
    )
    name=models.CharField(max_length=64,verbose_name='车名',blank=True,unique=True)
    colour=models.IntegerField(choices=COLOUR_CHOICES,default=0,verbose_name='颜色')
    price=models.DecimalField(max_digits=9,decimal_places=2,verbose_name='价格')
    image=models.ImageField(upload_to='img',default='img/default.png',verbose_name='图片')
    brand=models.CharField(max_length=64,verbose_name='牌子')

    @property
    def color(self):
        return self.get_colour_display()

    @property
    def picture(self):
        from django.conf import settings
        return '%s%s%s' %(settings.BASE_URL,settings.MEDIA_URL,self.image)
    class Meta:
        db_table='easy_crash_car'
        verbose_name_plural='汽车表'

    def __str__(self):
        return self.name
# views

# views.py
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from . import models,serializers


    def post(self, request, *args, **kwargs):
        if not isinstance(request.data, dict) or request.data == {}:
            return Response({
                'status': 0,
                'msg': '数据异常',
            })

        car_dser = serializers.CarDeSerializer(data=request.data)
        if car_dser.is_valid():
            car_obj = car_dser.save()
            return Response({
                'status': 0,
                'msg':'ok',
                'results':serializers.CarSerializer(car_obj).data
            })
        else:
            return Response({
                'status': 0,
                'msg': 'ok',
                'results':car_dser.errors
            })
# APIUrl

from django.conf.urls import url,include
from django.contrib import admin
from django.views.static import serve
from django.conf import settings

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^api/',include('api.urls')),
    url(r'^media/(?P<path>.*)',serve,{"document_root":settings.MEDIA_ROOT})

]
posted @ 2019-12-30 17:13  colacheng  阅读(83)  评论(0编辑  收藏  举报
Live2D