Django REST 应用app的建模

在项目创建和应用app创建之后,首先需要进行相应的建模

1.在computerapp文件夹中创建urls.py和serializes.py两个文件:

2.ajax跨域设置django-cors-headers

INSTALLED_APPS = [
   ...
    'rest_framework',
    'corsheaders',
    'computerapp.apps.ComputerappConfig',
    'django.contrib.staticfiles',
]


MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',#跨域请求设置
    ...
]


CORS_ORIGIN_ALLOW_ALL = True #允许跨域访问的域名

3.settings设置媒体文件存放位置

#文件存放的位置
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'eshop/media')

4.在eshop.urls中:

from django.conf.urls import url
from django.contrib import admin

from django.conf import settings
from django.conf.urls.static import static

#添加后台的url
urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

#添加调试模式时,媒体文件的相对路径
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

注意:添加后台的url,便于后台对数据的操作。添加调试模式时,媒体文件的相对路径,便于图片文件的测试。
5.建模思路:
- 用户模型
1. 默认用户模型(框架自带,无需自己建模)
2. 用户详情模型(根据自己需要的用户信息而建的模型,与用户模型是一对一的关系)
3. 收货地址模型(用户的收货地址信息的模型,与用户模型是一对多的关系)
- 产品模型
1. 产品模型(具体产品的各种信息模型)
2. 种类模型(产品所属的种类的模型,与产品是多对一的关系)
3. 厂商模型(产品的制造厂商的模型,与产品是多对一的关系)

from django.db import models

# Create your models here.


from django.conf import settings


class Category(models.Model):
    """
    商品类别:笔记本、平板电脑、一体机、台式机、服务器
    """  
    name = models.CharField(max_length=200)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name


class Manufacturer(models.Model):
    """
    生产厂商
    """  
    name = models.CharField(max_length=200)
    description = models.TextField()    
    logo = models.ImageField(blank=True, null=True, max_length=200, upload_to='manufacturer/uploads/%Y/%m/%d/')            
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name


class Product(models.Model):
    """
    产品
    """  
    model = models.CharField(max_length=200)
    description = models.TextField()  
    image = models.ImageField(max_length=200, upload_to='product/uploads/%Y/%m/%d/')        
    price = models.DecimalField(max_digits=12, decimal_places=2)
    sold = models.PositiveIntegerField(default=0)
    category = models.ForeignKey(Category, related_name='product_in', on_delete=models.DO_NOTHING)
    manufacturer = models.ForeignKey(Manufacturer, related_name='product_of', on_delete=models.DO_NOTHING)    
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.model



class DeliveryAddress(models.Model):
    """
    收货地址
    """  
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, related_name='delivery_address_of',)
    contact_person = models.CharField(max_length=200)
    contact_mobile_phone = models.CharField(max_length=200)
    delivery_address = models.TextField()  
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.delivery_address



class UserProfile(models.Model):
    """
    用户信息
    """  
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, related_name='profile_of',)
    mobile_phone = models.CharField(blank=True, null=True, max_length=200)
    nickname = models.CharField(blank=True, null=True, max_length=200)
    description = models.TextField(blank=True, null=True)  
    icon = models.ImageField(blank=True, null=True, max_length=200, upload_to='user/uploads/%Y/%m/%d/')        
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    delivery_address = models.ForeignKey(DeliveryAddress, related_name='user_delivery_address', on_delete=models.DO_NOTHING, blank=True, null=True,)

6.在eshop.admin文件中:

from django.contrib import admin

# Register your models here.

from computerapp.models import Product, Category, Manufacturer

class CategoryAdmin(admin.ModelAdmin):
    #网页中展示的种类信息
    list_display = ['id', 'name',]
#在admin后台注册该类,之后才会在后台显示出相应的模型
admin.site.register(Category, CategoryAdmin)



class ManufacturerAdmin(admin.ModelAdmin):
    list_display = ['id', 'name',]

admin.site.register(Manufacturer, ManufacturerAdmin)



class ProductAdmin(admin.ModelAdmin):
    list_display = ['id', 'model', 'price', 'category', 'manufacturer', 'sold',]
    #在展示的产品信息栏中可直接修改的字段
    list_editable = ['price', 'sold', 'category',]

admin.site.register(Product, ProductAdmin)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值