Vue + Django 2.0.6 Study Notes 6.1-6.2 merchandise category data interface

These two sections main point is that the details get goods category 123 class and a class list

I direct the bar code 

views.py:

Import CategorySerializer .serializers from


class CategoryViewset (mixins.ListModelMixin,
# inherit this class is to request the details of the front of a field are used.
# That only added to inherit this class elsewhere do not need to change, you can + id get through to get details of a field.
mixins.RetrieveModelMixin,
viewsets.GenericViewSet):

# this is a comment written description to rest_framework automatically generate documents automatically to the class of functions used

"" "
list:
Categories list data
"" "
QuerySet = GoodsCategory.objects.filter (= category_type. 1)
serializer_class = CategorySerializer
serializers.py

Import rest_framework serializers from

from goods.models Import Goods, GoodsCategory

class CategorySerializer3 (serializers.ModelSerializer):
class Meta -:
Model = GoodsCategory
Fields = "__all__ is"

class CategorySerializer2 (serializers.ModelSerializer):
# 2. Here look
# obtain three classifications anyway, it's that one level of nested get
sub_cat = CategorySerializer3 (MANY = True)
class Meta:
Model = GoodsCategory
Fields = "__all__"

class CategorySerializer (serializers.ModelSerializer):

# 1. look here
# this is the first layer sub_cat needs to be consistent with the model in the key fields the related_name
# so that we can get the data (that is, secondary classification) of the second layer
# must be added many = True because there may be some data there is no secondary or tertiary classification data

sub_cat = CategorySerializer2 (MANY = True)
class Meta:
= GoodsCategory Model
Fields = "__all__"
model.py just goes to show with.

GoodsCategory class (models.Model):
"" "
Product Category
" ""
CATEGORY_TYPE = (
(. 1, U 'a category'),
(2, U 'two categories'),
(. 3, U' three category ')
)
name = models.CharField (default =' ', 30 = MAX_LENGTH, the verbose_name = U' category name ', help_text = u' category name ')
code = models.CharField (default =' ', 30 = MAX_LENGTH , verbose_name = u 'category code', help_text = u 'category code')
desc = models.TextField (default = '', verbose_name = u 'category description', help_text = u 'category description')
category_type models.IntegerField = ( choices = CATEGORY_TYPE, verbose_name = u 'category level', help_text = u 'category level')

# To see here on this field because the field is empty it is necessary to set many = True in serializers there

parent_category = models.ForeignKey ( "self", null = True, blank = True, verbose_name = u ' parent class' , help_text = u 'parent class'
= the related_name 'sub_cat', on_delete = models.CASCADE)
is_tab = models.BooleanField (default = False, the verbose_name = U 'whether navigation', help_text = u 'whether navigation')
ADD_TIME = models.DateTimeField (default = DateTime.Now, verbose_name = u 'addition time')

class Meta -:
verbose_name = u 'product category'
verbose_name_plural the verbose_name =

DEF __str __ (Self):
return the self.name
This completes the return data interface product categories:

url:

 

 

Then it is certain of the details:

 

 

Ok. . From the beginning to learn to feel here. . . rest_framework good cock that I wrote so many VIEW before, form, url to what = =

5.9 to 5.8 are talking about the theory I wrote directly attached to a hierarchy, right now or foggy and other tutorial over again after re-read it over twice and then come back here and should be more effective

 

End
--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/10961720.html