Now, we will create a simple MessageModel
class that we will use to represent messages. Remember that we won't be persisting the model in the database, and therefore, in this case, our class will just provide the required attributes and no mapping information. Create a new models.py
file in the api
folder. The following lines show the code that creates a MessageModel
class in the api/models.py
file. The code file for the sample is included in the restful_python_chapter_05_01
folder:
class MessageModel: def __init__(self, message, duration, creation_date, message_category): # We will automatically generate the new id self.id = 0 self.message = message self.duration = duration self.creation_date = creation_date self.message_category = message_category self.printed_times = 0 self.printed_once = False
The MessageModel
class just declares a constructor, that is, the __init__
method. This method receives many arguments and then uses them to initialize the attributes with the same names: message
, duration
, creation_date
, and message_category
. The id
attribute is set to 0, printed_times
is set to 0
, and printed_once
is set to False
. We will automatically increment the identifier for each new message generated with API calls.
18.191.168.8