下载dvadmin
在gitee上搜索 django-vue-admin 别选错了,选择那个星最多的
gitee上会推荐移步 django-vue3-admin 别上当,用那个会踩坑,此教程就不适用了;
运行后端
后端
创建APP
python manage.py startapp rules
调整位置
为了管理方便 把刚创建的 rules 文件夹移动到dvadmin目录下
添加setting文件,在setting.py的INSTALLED_APPS下增加rules,由于刚移动了rules到dvadmin下所以就要使用 dvadmin.rules
调整位置后,还需要再调整apps的name不然报错说没有安装这个app
增加个url;rules在创建后是没有url.py的,需要手动增加一个,这个不用手动创建,直接从rules同级目录system下复制一份,并且把多余的代码注释掉,注释掉而不清理掉主要是为了方便后边增加模型后再增加路由
注释掉后的代码,只保留一个urlpatterns=[]
就行了,django会根据这个urlpatterns来找路由
rules的路由有了就能在总路由里配置上了
增加虚拟币币种表
model层:
在dvadmin/rules/models.py填充一下内容:
from django.db import models
from dvadmin.utils.models import CoreModel# Create your models here.table_prefix_in = "rules_"class MiningCryptoInfo(CoreModel):id = models.BigAutoField(primary_key=True, help_text="Id")# crypto_id = models.BigIntegerField(# verbose_name="虚拟币id", help_text='唯一标识,主键,自增,从80001开始', db_index=True, primary_key=True# )crypto_id = models.IntegerField(verbose_name="虚拟币id", help_text='唯一标识,主键,自增,从80001开始', )crypto_name = models.CharField(max_length=100, default=None, verbose_name="币种标准名", )crypto_code = models.CharField(max_length=20, default=None, verbose_name="币种代码", )refer_link = models.CharField(max_length=1024, default=None, verbose_name="参考链接", blank=True, )description = models.CharField(max_length=1024, default=None, verbose_name="币种描述信息", blank=True, )class Meta:db_table = table_prefix_in + "mining_crypto_info"verbose_name = '虚拟币币种'verbose_name_plural = verbose_nameordering = ('id',)
序列化
dvadmin有核心类库继承一下CustomModelSerializer就能省很多代码;
序列化放在了 backend/dvadmin/rules/views.py 文件下,直接复制过去就行了;
from dvadmin.utils.viewset import CustomModelViewSet
from dvadmin.rules.models import MiningCryptoInfo
from dvadmin.utils.serializers import CustomModelSerializer# Create your views here.class MiningCryptoInfoSerializer(CustomModelSerializer):"""序列化器"""class Meta:model = MiningCryptoInfofields = "__all__"
视图层
视图层上dvadmin也有核心类库继承一下CustomModelViewSet能省很多代码;
视图放在了 backend/dvadmin/rules/views.py 文件下,直接复制过去就行了;
class MiningCryptoInfoViewSet(CustomModelViewSet):queryset = MiningCryptoInfo.objects.all()serializer_class = MiningCryptoInfoSerializercreate_serializer_class = MiningCryptoInfoSerializerupdate_serializer_class = MiningCryptoInfoSerializerfilter_fields = ['id', 'crypto_id', 'crypto_name', 'crypto_code']search_fields = ['id', 'crypto_id', 'crypto_name', 'crypto_code']
在添加完序列化、视图层后的backend/dvadmin/rules/views.py 文件
from dvadmin.utils.viewset import CustomModelViewSet
from dvadmin.rules.models import MiningCryptoInfo
from dvadmin.utils.serializers import CustomModelSerializer# Create your views here.class MiningCryptoInfoSerializer(CustomModelSerializer):"""序列化器"""class Meta:model = MiningCryptoInfofields = "__all__"class MiningCryptoInfoViewSet(CustomModelViewSet):queryset = MiningCryptoInfo.objects.all()serializer_class = MiningCryptoInfoSerializercreate_serializer_class = MiningCryptoInfoSerializerupdate_serializer_class = MiningCryptoInfoSerializerfilter_fields = ['id', 'crypto_id', 'crypto_name', 'crypto_code']search_fields = ['id', 'crypto_id', 'crypto_name', 'crypto_code']
前端
前端创建一项
参考别处的代码直接复制一份过来;例如我复制了这个白名单的代码,并且改名为了XXX