z哎Django restframwork中就有一个类可以获取列表的详情内容,只有两行代码就可以搞定,在浏览器测试是ok的。但是这样的接口给前端,前端点击详情然后会将models.表名.objects.all()的所有内容重新放在了这个数组中。以至于前端点击详情拿到的并不是单个id对应的内容。所以稍微重写一下就好。看一下代码。本来就是一个数据表,列表页数据少,详情回去的数据多一点而已。
class Upload_filep(models.Model): """上传项目模型""" name = models.CharField(verbose_name='项目名', max_length=32) title = models.TextField(verbose_name='项目描述') upload_files = models.FileField(upload_to="upload/") img = models.ImageField(upload_to="upload/") address = models.CharField(max_length=64, verbose_name='地址', null=True, blank=True) add_time = models.DateTimeField(default=datetime.now, verbose_name='创建时间')
router = DefaultRouter() router.register(r'show_workPubs_detail',Mode_DetailViewSet,base_name="mode_detail") urlpatterns = [ path('admin/', admin.site.urls), url(r'^',include(router.urls)), ]+ static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
seiializers.py
class Show_workPublishedModelSerialiazer(serializers.ModelSerializer): class Meta: model = Upload_filep fields = ("id","name","title","img","upload_files","address") search_fields = ('name','title')
view.py(原Django restframwork中只写queryset和serializer_class就可以获取,但是前端拿到的并不是单个id对应的数据,所以重写get_queryset(self)就好)
class Mode_DetailViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): # 通过三维城市的id,获取三维城市的详情 queryset = Upload_filep.objects.all() serializer_class = Show_workPublishedModelSerialiazer def get_queryset(self): id = self.request.GET.get('id') if id is not None: return self.queryset.filter(id=id) return self.queryset
到此,列表详情的获取就可以完成