在使用Django过程中,为什么不用ModelSerializer,而用Serializer

用网页注册为例,在写代码的过程中发现:

1.接收的某些值不存在于模型类的属性中,需要额外定义。如确定密码,短信验证码等
2.验证方法需要自己写。
3.创建方法默认是将接收的值赋给属性,但是某些值不对应着属性,如密码需要加密后再赋值。

1. 使用serializerdjango template,可以通过调用drfserializer来渲染数据。首先,需要在view定义好serializer,并将查询到的数据序列化后传递给模板。例如: ```python from rest_framework import serializers class BookSerializer(serializers.ModelSerializer): class Meta: model = Book fields = ['id', 'title', 'author', 'publish_date'] def book_list(request): books = Book.objects.all() serializer = BookSerializer(books, many=True) return render(request, 'book_list.html', {'books': serializer.data}) ``` 然后,在模板使用django的模板语言展示序列化后的数据: ```html {% for book in books %} <div> <h3>{{ book.title }}</h3> <p>{{ book.author }}</p> <p>{{ book.publish_date }}</p> </div> {% endfor %} ``` 2. 使用Pagination 如果需要在django template使用drf的分页功能,可以通过调用Pagination类来实现。首先,需要在view定义好pagination,并将查询到的数据和pagination对象传递给模板。例如: ```python from rest_framework.pagination import PageNumberPagination class BookPagination(PageNumberPagination): page_size = 10 page_size_query_param = 'page_size' max_page_size = 100 def book_list(request): books = Book.objects.all() pagination = BookPagination() result_page = pagination.paginate_queryset(books, request) serializer = BookSerializer(result_page, many=True) return render(request, 'book_list.html', {'books': serializer.data, 'paginator': pagination}) ``` 然后,在模板使用django的模板语言展示分页后的数据: ```html {% for book in books %} <div> <h3>{{ book.title }}</h3> <p>{{ book.author }}</p> <p>{{ book.publish_date }}</p> </div> {% endfor %} <div class="pagination"> <span class="step-links"> {% if page_obj.has_previous %} <a href="?page=1">« first</a> <a href="?page={{ page_obj.previous_page_number }}">previous</a> {% endif %} <span class="current-page"> Page {{ page_obj.number }} of {{ paginator.num_pages }} </span> {% if page_obj.has_next %} <a href="?page={{ page_obj.next_page_number }}">next</a> <a href="?page={{ paginator.num_pages }}">last »</a> {% endif %} </span> </div> ``` 3. 使用Filter 如果需要在django template使用drf的过滤功能,可以通过调用FilterSet类来实现。首先,需要在view定义好filter,并将查询到的数据和filter对象传递给模板。例如: ```python from django_filters import rest_framework as filters class BookFilter(filters.FilterSet): title = filters.CharFilter(lookup_expr='icontains') author = filters.CharFilter(lookup_expr='icontains') class Meta: model = Book fields = ['title', 'author'] def book_list(request): books = Book.objects.all() book_filter = BookFilter(request.GET, queryset=books) serializer = BookSerializer(book_filter.qs, many=True) return render(request, 'book_list.html', {'books': serializer.data, 'filter': book_filter}) ``` 然后,在模板使用django的模板语言展示过滤后的数据: ```html <form method="get"> <label for="title">Title:</label> <input type="text" id="title" name="title" value="{{ filter.form.title.value }}"> <label for="author">Author:</label> <input type="text" id="author" name="author" value="{{ filter.form.author.value }}"> <button type="submit">Filter</button> </form> {% for book in books %} <div> <h3>{{ book.title }}</h3> <p>{{ book.author }}</p> <p>{{ book.publish_date }}</p> </div> {% endfor %} ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值