WEBVTT
Kind: captions
Language: en

00:00:00.030 --> 00:00:05.520
Now that we have created our API view we
can go ahead and add a serializer to our

00:00:05.520 --> 00:00:11.280
view a serializer is a feature from the
Django rest framework that allows you to

00:00:11.280 --> 00:00:18.060
easily convert data inputs into Python
objects and vice versa it's kind of

00:00:18.060 --> 00:00:23.550
similar to a django form which you
define and it has the various fields

00:00:23.550 --> 00:00:27.840
that you want to accept for the input
for your api so if we're going to add

00:00:27.840 --> 00:00:33.840
post or update functionality to our
hello API view then we need to create a

00:00:33.840 --> 00:00:39.420
serializer to receive the content
that we post to the API let's go ahead

00:00:39.420 --> 00:00:44.540
and create the sterilizer for our
profiles API project

00:00:44.540 --> 00:00:46.020
Create a new file

00:00:46.020 --> 00:00:53.360
inside our profiles API app and call it
serializers dot py

00:00:53.360 --> 00:00:54.320
It's good practice to

00:00:54.329 --> 00:00:58.739
keep all of your sterilizers for your
app in a single file called serializers

00:00:58.739 --> 00:01:02.820
so you know where to look if you ever
need to find them we're going to start

00:01:02.820 --> 00:01:08.490
by including the serializers import
from the Django rest framework so type

00:01:08.490 --> 00:01:16.439
from rest underscore framework import
serializers then we're going to create a

00:01:16.439 --> 00:01:22.049
new class called hello serializer and
we're going to base it on the serializer

00:01:22.049 --> 00:01:26.210
class from the Django rest framework so
type class

00:01:26.210 --> 00:01:32.790
hello serializer
and then in the base class we're going to

00:01:32.790 --> 00:01:39.030
type serializers dot serializer and the
second one is the class name so it's got

00:01:39.030 --> 00:01:41.160
a capital S

00:01:41.160 --> 00:01:46.920
Okay then put a dock string
under the class declaration called and

00:01:46.920 --> 00:01:55.549
write serializes a name
field for testing our

00:01:55.549 --> 00:01:58.980
API view

00:01:58.980 --> 00:02:01.920
What we're going to do is we're
going to create a simple serializer that

00:02:01.920 --> 00:02:07.020
accepts a name input and then we're
going to add this to our API view and

00:02:07.020 --> 00:02:12.560
we're going to use it to test the post
functionality of our API view

00:02:12.560 --> 00:02:17.640
serializers function very similar to Django forms if you've

00:02:17.640 --> 00:02:21.299
ever used them if you haven't use them
don't worry they're quite simple to get

00:02:21.299 --> 00:02:24.690
your head around all you do is you
define the serializer and then you

00:02:24.690 --> 00:02:30.599
specify the fields that you want to
accept in your serializer input so we're

00:02:30.599 --> 00:02:35.220
going to create a field called name and
this is a value that can be passed into

00:02:35.220 --> 00:02:39.780
the request that will be validated by
the serializer

00:02:39.780 --> 00:02:41.220
So sterilizers also take

00:02:41.220 --> 00:02:45.299
care of validation rules so if you want
to say you want to accept a certain

00:02:45.299 --> 00:02:50.099
field of a certain type serializers will
make sure that the content past that api

00:02:50.099 --> 00:02:54.596
is of the correct type that you want to
require for that field

00:02:54.596 --> 00:02:55.829
So let's define a

00:02:55.829 --> 00:03:03.410
new field which is a character field for
a name input so type name equals

00:03:03.410 --> 00:03:10.519
serializers dot char field and then we
need to pass in a maximum length

00:03:10.519 --> 00:03:16.440
argument like max length equals 10

00:03:16.440 --> 00:03:18.329
what this does is it creates a new name

00:03:18.329 --> 00:03:23.160
field on our serializers which is a
character field that allows you to input

00:03:23.160 --> 00:03:28.650
any text that you can input on the
computer and it gives it a maximum

00:03:28.650 --> 00:03:33.359
length of 10 so it will accept any
character inputs

00:03:33.359 --> 00:03:37.580
that are 10 or less characters

00:03:37.580 --> 00:03:40.680
Okay so save that file and that's how

00:03:40.680 --> 00:03:45.590
you create a serializer with the Django
rest framework

