Home > Software design >  Will creating serializer.Serializer fields saves to the database?
Will creating serializer.Serializer fields saves to the database?

Time:01-06

When we're not creating models and directly creating the fields inn serializers using serializer.Serializer , will the fields save to database? because we havent migrated to the database? also if i have created one existing database, i'm creating some additional fields in serializers? will that also saves to database? can anyone lighten me up? Im new to django api. here i provide some example.

let my Model.Py be like,

 class Detail(models.Model):
    fname = models.Charfield(maxlength=20)
    lname = models.Charfield(maxlength=20)
    mobile = models.IntergerField(maxlength=20)
    email = models.EmailField(maxlength=20)

let my Serializer.Py be like,

 class DetailSerializer(serializer.Serializer):
      created_at = serializer.Charfield
      is_active  = serializer.Booleanfield

Will this serializer save in database that i created manually in serializer? another question is if i create serializers without a model, will that save to database? if that saves in database how's it possible without migrating?

CodePudding user response:

No.

DRF won't create a field in your model table if you declare a field that doesn't exists in your model.

Actually, when using ModelSerializer and declare a field that does not exists in your model, you will get an error like django.core.exceptions.ImproperlyConfigured: Field name created_at is not valid for model Detail. So you won't be able to use your serializer at all.

And when you are using Serializer, you don't have create method so you won't be able to save data on db at all.

  • Related