Model, Form and DTO of PWeb CRUD Example
Here will describe about Model is Schema of DB, Form for Data Validation and serialization, lastly DTO for Data validation, serialization, deserialization of API.
Model
from pweb_orm import PwebModel, pweb_orm
class Person(PwebModel):
name = pweb_orm.Column("name", pweb_orm.String(150), nullable=False)
email = pweb_orm.Column("email", pweb_orm.String(150), nullable=False)
password = pweb_orm.Column("password", pweb_orm.String(250), nullable=False)
address = pweb_orm.Column("address", pweb_orm.Text())
Form
from boot.model.person import Person
from pweb_form_rest import fields, PWebForm
class PersonDetailsForm(PWebForm):
class Meta:
model = Person
load_instance = True
name = fields.String(required=True, error_messages={"required": "Please enter name"})
email = fields.Email(required=True, error_messages={"required": "Please enter email"})
address = fields.String(allow_none=True, type="textarea")
class PersonCreateForm(PersonDetailsForm):
class Meta:
model = Person
load_instance = True
password = fields.String(required=True, error_messages={"required": "Please enter password"}, type="password")
class PersonUpdateForm(PersonDetailsForm):
class Meta:
model = Person
load_instance = True
id = fields.Integer(required=True, error_messages={"required": "Please enter id"}, type="hidden", isLabel=False)
-
Note : Must Inherit from
PWebForm
DTO
from boot.model.person import Person
from pweb_form_rest import fields, PWebRestDTO
class PersonDetailsDTO(PWebRestDTO):
class Meta:
model = Person
load_instance = True
name = fields.String(required=True, error_messages={"required": "Please enter name"})
email = fields.Email(required=True, error_messages={"required": "Please enter email"})
address = fields.String(allow_none=True)
class PersonCreateDTO(PersonDetailsDTO):
class Meta:
model = Person
load_instance = True
password = fields.String(required=True, error_messages={"required": "Please enter password"})
class PersonUpdateDTO(PersonDetailsDTO):
class Meta:
model = Person
load_instance = True
id = fields.Integer(required=True, error_messages={"required": "Please enter id"})
-
Note : Must Inherit from
PWebRestDTO