当前位置:网站首页>Rewriting method set
Rewriting method set
2022-07-29 01:13:00 【:D...】
Catalog
One 、 View set overrides create
1 Judge the front-end information Avoid repeated addition
(1) Deserialize add data
Use when there is a large amount of data to be added
# Avoid adding problems repeatedly
def create(self, request, *args, **kwargs):
c_name = request.data.get('name')
c_coupon_type = request.data.get('coupon_type')
num = Coupon.objects.filter(name=c_name, coupon_type_id=c_coupon_type).count()
if num > 0:
return Response({
'msg': ' This coupon has been added '}, status=400)
else:
# Deserialize add data
ser = self.get_serializer(data=request.data)
ser.is_valid()
ser.save()
return Response({
'msg': ' Add success '}, status=201)
(2) Normal add data
Suitable for small amount of data
# Duplicate additions are not allowed
def create(self, request, *args, **kwargs, ):
name = request.data.get('value')
spec = request.data.get('spec')
# print('$$$$$$$ Get objects ->', name, '$$$$$$$ type ->', type(name),)
num = SpecOption.objects.filter(value=name).count()
if num > 0:
return Response({
'msg': " Add repeatedly ", 'status': 400})
else:
SpecOption.objects.create(value=name, spec_id=spec)
return Response({
'msg': " Add success ", 'status': 201})
2 Serializer verification increases , Judge whether the information contains xx
#SPU An increase in increases , Pay attention to judgment SPU Does the name contain Xiaomi ,
def validate_name(self, name):
if ' millet ' in name:
return name
else:
raise ValidationError('SPU The name does not include Xiaomi , Add failure ')
Two 、destroy
1 Only delete... Is allowed contain xx Of SPU
# SPU The deletion of , Only delete names that contain Millet TV Of SPU
def destroy(self, request, *args, **kwargs):
spu = self.get_object()
print('xxxxxxxxxx>>>>>>>', spu,'xxxxxxxxxx>>>>>>>', type(spu), spu.name)
name = spu.name
if ' Millet TV ' in name:
SPU.objects.get(name=name).delete()
return Response({
'msg': ' Delete successful ', 'status': 201})
return Response({
'msg': ' The name does not include Xiaomi TV , Delete failed ', 'status': 400})
2 Only delete... Is allowed The foreign key field contains xx Of
# Only delete... Is allowed spu The name of Xiaomi has the specification of Xiaomi ;
def destroy(self, request, *args, **kwargs):
spec = self.get_object()
print('xxxxxxxxxx>>>>>>>', spec,'xxxxxxxxxx>>>>>>>', type(spec), spec.spu, spec.name, spec.id)
if ' millet ' in spec.spu.name:
idq = spec.id
SpuSpecification.objects.get(id=idq).delete()
return Response({
'msg': ' Delete successful ', 'status': 201})
return Response({
'msg': ' The name does not include Xiaomi TV , Delete failed ', 'status': 400})
3、 ... and 、update
1 Modify only xx Fields containing numeric
# Only specification values containing values can be modified ! Such as " aluminium alloy " It's not allowed to modify , and "181g" Allow modification .
def update(self, request, *args, **kwargs):
option = self.get_object()
value = option.value
n_value = request.data.get('value')
n_spec = request.data.get('spec')
num_in = re.search(r'\d', value)
if num_in:
option.value = n_value
option.spec_id = n_spec
option.save()
return Response({
'msg': " Include values , Modification successful "}, status=201)
else:
return Response({
'msg': " Does not contain values , Modification failed "}, status=400)
2 When the front end submits the modification , Deserialize the inbound fields plus other information
# SPU modify When the front end submits the modification , When deserializing warehousing ,SPU Add your own name after your name ,
# such as millet 10, Deserialization warehousing is millet 10_jack modify .
def update(self, request, *args, **kwargs):
spu = self.get_object()
# print('xxxxxxx>>>>>>', spu, type(spu), '<<<<<', spu.desc_detail)
name = request.data.get('name')
spu.name = name+'_luoting modify '
spu.brand_id = request.data.get('brand')
spu.category1_id = request.data.get('category1')
spu.category2_id = request.data.get('category2')
spu.category3_id = request.data.get('category3')
spu.desc_detail = request.data.get('desc_detail')
spu.desc_pack = request.data.get('desc_pack')
spu.desc_service = request.data.get('desc_service')
spu.brand_name = request.data.get('brand_name')
spu.save()
return Response({
'status': 200, 'message': 'SPU Modification successful '})
3 Modify the simple process
# rewrite update
def update(self, request, *args, **kwargs):
# 1 Take over front-end data
status = request.data.get('status')
# 2 Get the object to update
order = self.get_object()
# 3 Update status
order.status = status
order.save()
return Response({
'code':200, 'msg': ' Update status succeeded '})
Four list
1 Show subclasses
# Get subcategories 2,3 channel/categories/
class SubsCate(ListAPIView):
queryset = Cate.objects.all()
serializer_class = Cate_Serializer
lookup_field = 'pk'
def list(self, request, *args, **kwargs):
cate_subs = self.get_object().subs.all() # object .subs.all() The reverse query method defined by the model class
ser = Cate_Serializer(cate_subs, many=True)
return Response(ser.data)
5、 ... and retrieve
1 Return the requested json data
# rewrite retrieve retrieval Return the requested json data
def retrieve(self, request, *args, **kwargs):
order = self.get_object()
order_ser = OrderInfo_Serializer(order)
order_goods = order.orderGoods.all()
order_goods_ser = OrderGood_Serializer(order_goods, many=True)
temp = []
for i in order_goods_ser.data:
i['sku'] = Good_Serializer(Good.objects.filter(id=i.get('sku')).first()).data
# Save to a temporary list
temp.append(i)
order_dict = order_ser.data
order_dict['skus'] = temp
return Response(order_dict)
6、 ... and Involving users Add modification Need encryption
The article links
http://t.csdn.cn/JdqI5
边栏推荐
- PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
- [web development] basic knowledge of flask framework
- Naver 三方登录
- Talk about the cross end technical scheme
- 【刷题笔记】链表内指定区间反转
- (update 20211130) about the download and installation of Jupiter notebook and its own configuration and theme
- Hash table~
- In the second round, 1000 okaleido tiger were sold out in one hour after logging in to binance NFT again
- Return the member function of *this
- Recursion and divide and conquer
猜你喜欢
随机推荐
DDD领域驱动设计如何进行工程化落地
Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network
数字孪生轨道交通:“智慧化”监控疏通城市运行痛点
FLV文件简介
新拟态个人引导页源码
Definition of double linked list~
B+ 树 ~
对接支付宝支付
[Commons lang3 topic] 002 randomutils topic
面试突击69:TCP 可靠吗?为什么?
[notes for question brushing] specified interval reversal in the linked list
Hilbert transform and instantaneous frequency
Daniel guild Games: summary and future outlook of this year
ActiveMQ基本详解
Wechat campus bathroom reservation for the finished product of applet graduation design (7) mid term inspection report
Deep learning | matlab implementation of TCN time convolution neural network spatialdropoutlayer parameter description
(perfect solution) why is the effect of using train mode on the train/val/test dataset good, but it is all very poor in Eval mode
Transfer: cognitive subculture
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
RHCE命令练习(二)









