alright guys welcome back and in this
video I am going to show you how to make
a serializer class so before we get
started let's figure out what the heck a
serializer class is and the most
important question does it have anything
to do with cereal the best by the way is
Cinnamon Toast Crunch the answer to that
is yes it does
I'm just getting a dozen all right so a
serializer class is a class that's going
to convert this model to json data
because remember right now whenever the
user requests a website we usually just
send them back regular old HTML in the
HTTP response however we don't want to
send them HTML that's only for whenever
you're want information from a browser
what we want to do is we want to send
them jason so we need a way to convert
this to jason and that's what to
serialize your classes that's all it is
whenever you read the documentation they
make it sound like it's something
incredibly complicated it's not so I'm
just going to name this class
serializers and it's going to go right
in my company's app right there and in
order to do this we actually need to
import some stuff from the rest
framework so from rest framework import
serializers and also we need to import
whatever models we want to serialize by
the way whenever you hear the term
serialized that just means taking a
model and converting it to some data
that you can actually either just save
on a hard drive or like transfer across
the network because the reason that we
need to serialize things is let's say
that we have these stock objects well
you can use them in our program but what
happens when we want to save them to a
thumb drive and give them to our friends
well we just can't save Python objects
to a thumb drive so that's why we need
to serialize it basically saving data in
a format that you can transfer so that's
it all right where was I so from models
import stock alright so the common class
name for this is just whatever your
model name is which stock
followed by serializer
make sure i spell everything correctly
alright so whatever object you're trying
to serialize just say that object
serializer simple enough so now you
actually need to inherit from
serializers dot model serializer and
again this is all built-in you get it
from the rest framework basically we're
gonna be converting something to jason
based on a model so this is the
blueprint that we're going to use in
other words whenever we send this jason
response it knows to include the ticker
open/close and volume so all those
pieces of information we don't have to
specify it so now what we can do is we
can make class meta and we can actually
further configure these so the first
thing you need to do is you actually
need to say what model you're trying to
serialize so I know that we only have
one in here right now but let's say that
we have one for stocks and companies and
users and pictures whatever you can
actually write all your classes in the
same file so that's why you need to
specify the specific one right here now
the next thing you specify in here are
whenever the user requests information
about stocks what attributes do you want
to return so you can either return all
of them right here or maybe you have
some weird ID number or maybe some I
don't know formula about stocks you're
working on and you don't want to send it
to the user let's say that you only
wanted to send them back the ticker and
the volume so how would you specify that
well if you use the keyword fields then
go ahead and add whatever attributes you
want to send back so I'll just rate
ticker and volume so just like that so
then again whenever they make a request
that says hey give me information about
Facebook it's only going to send back
their ticker and volume and not their
open or closed now a cool little
shortcut is if you want to return
everything then what you can do is you
can go in here and just type too
open clothes volume or a shorter way is
if you just go fields it's given me a
red line that I spelled it wrong all
right so dunder all done der so double
underscore all double underscore and
again the reason that you may not want
to do this is if you have some like
weird primary keys or like ID number for
the user that you don't want them to see
whatever but in this example I'm just
going to go ahead and send back all of
those fields
boom roasted