Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement to simplify the BaseModel boilerplate code #2637

Closed
James4Ever0 opened this issue Oct 23, 2022 · 3 comments
Closed

enhancement to simplify the BaseModel boilerplate code #2637

James4Ever0 opened this issue Oct 23, 2022 · 3 comments

Comments

@James4Ever0
Copy link

i wrote some code for simplification:

from peewee import *

# some patch on /usr/local/lib/python3.9/dist-packages/peewee.py:3142

# @property
# def Model(self): # this is interesting. does it work as expected?
#     class BaseModel(Model):
#         class Meta:
#             database = self
#     return BaseModel

db = SqliteDatabase('my_database.db')
class User(db.Model):
    username = CharField(unique=True)
@James4Ever0
Copy link
Author

James4Ever0 commented Oct 23, 2022

ps: i've seen this pattern from sqla-wrapper. this library might be helpful as well.

@James4Ever0
Copy link
Author

James4Ever0 commented Oct 23, 2022

another enhancement:

# /usr/local/lib/python3.9/dist-packages/peewee.py:3420

#   def create_tables(self, models, bind=True,**options):
#       for model in sort_models(models):
#          if bind:
#              model.bind(self)
#          model.create_table(**options)
class User2(Model):
    username = CharField(unique=True)

db.create_tables([User2])

@coleifer
Copy link
Owner

I'm going to pass on the create_tables() but have merged the Model property helper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants