Prepare Data for Front End Dynamic Drop Down

Let’s assume we will create supplier group drop-down data for front end


Create DTO for the drop-down

class SupplierGroupDropDownList(PfBaseSchema):

    class Meta:
        model = SupplierGroup
        load_instance = True

    id = fields.Integer(required=True, error_messages={"required": "Please enter id"})
    name = fields.String(required=True, error_messages={"required": "Please enter name"})
Here
  • PfBaseSchema : Base schema


Create drop_down_list method on SupplierGroup Service

pfs_rest_helper_service = PfsRestHelperService(SupplierGroup)


class SupplierGroupService(PfRequestResponse):

    ....

    def drop_down_list(self):
        query = SupplierGroup.query.with_entities(SupplierGroup.name, SupplierGroup.id)
        return pfs_rest_helper_service.rest_list(SupplierGroupDropDownList(), model=query, pagination=False)

    ....
Here
  • pfs_rest_helper_service : Import PFS Rest helper service

  • SupplierGroup.query.with_entities(SupplierGroup.name, SupplierGroup.id) : Select name and id


Create Controller method for the drop-down

@supplier_group.route("/drop-down-list", methods=['GET'])
@request_response_list(response_obj=SupplierGroupDropDownList)
def drop_down_list():
    return supplier_group_service.drop_down_list()
Here
  • @supplier_group : Blueprint name

  • @request_response_list : Swagger Definition with the DTO

  • supplier_group_service.drop_down_list() : Call the drop-down method from Supplier Group Service