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.pypython
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
到此,列表詳情的獲取就能夠完成瀏覽器