首先在settings.py中引入
INSTALLED_APPS = [
......
'rest_framework',
......
]
相关模型结构如下:
class custume(models.Model):
'''服装护具类'''
interno = models.CharField(max_length=50, verbose_name='内部商品编号', help_text='可以是数字或字母组合')
name = models.CharField(max_length=32, verbose_name='品名')
sort = models.ForeignKey(bm.custume_sort, verbose_name='衣着分类',on_delete=models.CASCADE,default=1)
supplier = models.ForeignKey(bm.supplier, verbose_name='供应商', on_delete=models.CASCADE, default=1)
brief = models.TextField(max_length=255,verbose_name='商品简述')
perunit = models.ForeignKey(bm.perunit,on_delete=models.CASCADE,verbose_name='计价单位',default=1)
reference_price = models.SmallIntegerField(verbose_name='参考价', help_text='随着价格变动,可能略有涨幅,以大佬定价为主',default=0)
class Meta:
db_table = 'custume'
def __str__(self):
return self.name
class supplier(models.Model):
'''供货商 '''
name = models.CharField(verbose_name='供货商名称', max_length=32)
contact = models.CharField(verbose_name='联系人', max_length=32)
sex = models.ForeignKey(gender,verbose_name='性别',on_delete=models.CASCADE,default=1)
def __str__(self):
return self.name
class Meta:
db_table = 'supplier'
在模块内新建site-package:
serializer.py中定义相关模型serializer
class soxserializer(serializers.Serializer):
name = serializers.CharField()
interno = serializers.CharField()
supplier = serializers.CharField(source = "supplier.name")#外键字段
在views.py中定义相关表的接口类
class soxer(ModelViewSet):
queryset = sm.custume.objects.all()
serializer_class = soxserializer
在urls.py中定义相关表的url类
引入相关支持模块
from django.conf.urls import url, include
from rest_framework import routers
from shoppingcenter import views as sv
router = routers.DefaultRouter()
router.register(r'saiwa',sv.soxer)
urlpatterns = [
......
url(r'^',include(router.urls)),
......
]