Here we will show some methods that the Model has and we will also see how to
use each one of them.
To run the examples run using ipython.
get_name
Method that returns the name of the Model in the database.
And if the __tablename__ attribute has not been assigned in the Model, then
this method will return the class name in lowercase.
1 2 3 4 5 6 7 8 910111213141516
classPerson(Model):__db__=dbid:int=Field.Integer(primary_key=True,auto_increment=True)first_name:str=Field.String(unique=True)Person.get_name()# will return 'person'.classClient(Model):__tablename__='clients'__db__=db# definitions of fields...Client.get_name()# Will return 'clients'.
create
Asynchronous method that will create the table that represents the Model
in the database.
It should always be used before trying to save, search or delete an object
of the Model.
Asynchronous method that retrieves all objects persisted in a table in the
database.
Parameters:
fields_includes: The Model fields that are to be retrieved.
fields_excludes: The Model fields that should not be retrieved.
conditions: Conditions for filtering objects.
limit: The maximum limit of objects that must be retrieved.
1 2 3 4 5 6 7 8 91011121314151617181920
...fromduck_orm.sql.conditionimportConditionpersons:list[Person]=awaitPerson.find_all(fields_includes=['first_name','age','salary'],# I could have left this parameter blank as it is no longer in # fields_includes.fields_excludes=['id'],conditions=[Condition('first_name','LIKE','Teste%'),Condition('salary','>=',2600)])forpersoninpersons:print(person.id)# None in all.print(person.first_name)# Teste 1, Teste 2print(person.age)# 19, 25print(person.salary)# 5000, 4000
Asynchronous method that retrieves only one object persisted in a table in the
database. If you pass the filtering condition by the key field, this
method can be used as a find by id.
Parameters:
fields_includes: The Model fields that are to be retrieved.
fields_excludes: The Model fields that should not be retrieved.
conditions: The conditions for filtering the object.
1 2 3 4 5 6 7 8 91011
person:Person=awaitPerson.find_one(conditions=[Condition('id','=',1)])print(person.id)# 1print(person.first_name)# Teste 1print(person.last_name)# teste lastnameprint(person.age)# 19print(person.salary)# 5000
fields_includes: The Model fields that are to be retrieved.
fields_excludes: The Model fields that should not be retrieved.
conditions: The conditions for filtering the object.
1234567
person:Person=awaitPerson.find_by_id(1)print(person.id)# 1print(person.first_name)# Teste 1print(person.last_name)# teste lastnameprint(person.age)# 19print(person.salary)# 5000
find_all_tables
1
asyncdeffind_all_tables():
Asynchronous method that returns all table names persisted in the database.
1
print(awaitPerson.find_all_tables())
update
1
asyncdefupdate(**kwargs)->Model:
Asynchronous method to alter a record persisted in the database.
Parameters:
kwargs: A dictionary with the Model fields that must be changed
and its new values.
1 2 3 4 5 6 7 8 910111213
person:Person=awaitPerson.find_one(conditions=[Condition('id','=',1)])print(person.id)# 1person:Person=awaitperson.update(first_name='Teste 1 UPDATE',age=22)print(person.id)# 1print(person.first_name)# Teste 1 UPDATEprint(person.age)# 22
delete
1
asyncdefdelete(conditions:List[Condition]):
Asynchronous method that deletes a record from the database.
Parameters:
conditions: The conditions for filtering the record(s).