Overview

In the following article, I confirmed the basic operations of Django REST framework JSON:API (DJA).

In this article, I will try adding a custom model to DJA.

References

I will add a UserInfo model, referencing the following article.

https://tech-blog.rakus.co.jp/entry/20220329/python

Steps

Define the Model

Add the following:

#classubacsigrUereesrtae_h=trn_eIadmdnmao_feydaoet(==lBs=amm.sooPmeddooMeesdolliedsstle..islCDv.)haeD:atSaremtFFaeiilTeelillImddne((tFvveieegerrelbbrdooF(ssiveeee__lrnndbaa(ommvseeee==r_''bnoasmee_=''n',)ammaex=_''l,ean'ug,ttnohu_=ln3lo2=w)T_rauded,=uTnriuqeu)e=False)###

Build the Database

Execute the following:

%M%ORipudgedeAnAjrxjrpnpaaa-aapipntmntlnlgipCgiygyoolrooi-nee-namnas/aasligdmtdlgmfiemtreiogiomaxnrrmnitaaopgimm'tdmeropaeieiranlkxolgftseeanroi:.mmsUaro0ip/stmn0gl0ee:s1re0r:3a'1I_t:3n-aui_fsusouoetensthrset,irinincf-ngoosfsn.eo=t.t.ee.tpxniyatOnmtKgpyslp=eee.sxs,aemteptxliaenm.gpssleet,tisnegsssions,sites

For reference, the following file is created:

#fcrlGoaemsnsd]o]edeprjMpeaaiertngn(am)egrd"ti,doaeeig.tnxorbdicananf]o}yboimstai,p,nepimetDi(sl=oelijmmen=d)(((()o"api="[s"s,"""",nanog,.U=mubasbgrr[Cs[osig=sota"re"m)dere"m{tt0erio,irt"cor4mi0aIddf_h,rda.io1tn"ein_eec1gn2ef,lapsveadmalt.rs_Mosureedmaots"8a.ao".tirr_eyde.:tMud,Aomiba""edDoiiteu_aaot,,l_aFnoghltcrls"satanro(oryie,mm.tel2sarFe_z_ooP"Ts0,t_iakenmddo,ie2ifete=aoeesm,3mouleyFmdllie-onldd=aeesstF0d)l(=Tl=l..ii6e:_Trs"sCDve-lnrueI.hael0saue,DDatSd4me,"arem(e,,tFFaa1"eiilu7)Teelt:,illIo3mddn_5e((tnFmveoiaegwexre_l_bradloFd(esidanee=ug_lTttndroha(u_=mnen3eu,o2=lw,"lv==eTvTrrerburuoeb"es)o),e)s)_,e,vn_eanrmabemo=es"=e"_nam"e)"=,)"),")),

Create Components

Serializer

Add the following:

fccrlloaamssUscs"D"cesl"R"lx.eUaU"F"aa,rssssmIesmfedsmfpnroireoilfIMdeIfMdeeoneelnaeel.ftldfutldmoasolasoS:=Dt:=de=R=erUFsUlisSessae'ere'lriririiiIdiaIdmzn'aln'pef,lif,oro'izo'r(uzeutsserseere(rr(tri_do_anrnlaftaim_emzessee'et'r,r,sid.'le'MbafboizaidreuretrltlhsthS_._edMDdraoRaiydFya'e'l,lf,i'Su'zaenaegrcgreite)'ai':,lo,'in'czacrelreriea)tat:iteeedsd__aatt''))

ViewSets

Define the View class.

ffcrrlooammsUUsqsdesesueex.ex.eUerfa,ra,rsrimImIeyageirpnpnrslenfelflfIeittteoeontz_reu..Sfeoynrrmseo=rb_tenoerV_jprtdriiUcekyuseiaeslc_rulalweat=pnpslisrs(keizeIsssErizetneein(mer(f=llst)prMoffr.oso.U).nygrdos:ko.etiebewtotmljrab_(pVeIrNjooicngoebretfsncjtwso.eteS.Sg:sc(eaee.ttlrtg()li(e):(a"t)le(iniztder=rye_nptkr"y,_pNko)n.eb)log

Router

Add the following:

frrrroooouuumtttAUeeeeusrrrxte..ahr=rrmoIeeprnrgglRfoiieeouss.lVtttvaieeeiterrreiws((wos.rrsneD""stebuihflsmiaoeppugroVls-rit"iteR,nwof(,uBotl"eo,rg(VUtisreeawriSIlenitfn)ogV_iselwasseht=)False)

Results

Accessing http://localhost:8000/ shows that user-info has been created.

The list was also retrievable at http://localhost:8000/user-info.

Summary

We hope this serves as a useful reference for using DJA.