Overview

Let’s customize the views for the model added in the following article.

Sort

Let’s add ordering_fields.

classoqsdrueeUderfserieryageirrislenfeIneitttngtz_reuf_eoynrrof=rb_tenVi_jprtieUcekyuselslc_ruwdeat=pnpssrs(keeIsssErt=neein((f=llst)M(offr.o".U).nygduos:ko.eesbewtotlejrab_VreIrNjoi_cngoebentfsncjwaso.eteSm.Sg:sceeaee.tt"lrtg(),li(e):(a"t))le(ini#ztder=Aryed_ndptekrd"y,_hpeNkro)en.eb)log

As a result, only user_name became selectable in the “Filters” display.

For example, sorting by age returned a validation error.

Filter

classqso###frf}suerieieUerdAwlllasriedvitstreyardeteQODS"""""""""""e""crslierhrurje=eicigglliririuhIeindro_edaaxeocttttnessds_ntzgiubrenr(axno"e"e"gne"effe_fdtayrgccatn,",",eut:rio=rfrecPiohtcat,,xl__eV_iobkanFF"tia"lf(nliUcemtrergii,"ni,"i"adesllhenaFll,sn,eemsweadheadmitt"slxesrssekselee,"da"=eIsrdittrr,sc:tn=een=eeB,t((f=fgrra="r"Mo(ba(V,c,euo.U"euoak{lsdosulllle"seebesotdinirljreweddn_VeIrfra,"nicn_it)aetfnlui,mwsoatsoeS.Smean"eaeergF,tlr"ei)li,bl):(aaot)l)cfeikrzen,enordns-sitnanodradredrqtuoertyesptarQaumesrylPiakream`eptaegreV_asliizdea`t.ionFilter

With the above, the following filter became possible.

http://localhost:8000/user-info?filter[user_name.contains]=nakamura

For id, since only exact and in are allowed, the following resulted in a validation error.

http://localhost:8000/user-info?filter[id.contains]=2

Checking the swagger-ui confirmed that filter was correctly configured.

http://localhost:8000/swagger-ui/

ReadOnlyModelViewSet

This appears to be a Django REST framework feature, but by using ReadOnlyModelViewSet, I was able to create a view-only view.

classqsouerUerdsrieeyarrsliIeinntzgfe_o=rfV_iiUceesllweadsrsseIstn=(f=Ro(e.U"aosudbesOjreneIrlcn_ytfnMsoao.Smdaeeelr"lli,V(ai)l)eiwzSeert):#Changedhere

Checking the swagger-ui confirmed that only get was set.

Summary

There are many points where my understanding is still insufficient, but I hope this serves as a useful reference for implementing filters.