私はpeewee(ORM)を使って小さなPython-MySQLアプリケーションを作成しています。次のように 私のコードは、単一のファイルで完璧に動作します:ピーウィーを使用して定義されているよう複数のPythonクラスを別々のメインPythonファイルにリンクするにはどうすればいいですか?
import os
from peewee import *
from playhouse.db_url import connect
# Connect to the database URL defined in the environment, falling
# back to a local MySql database if no database URL is specified.
db = connect(os.environ.get('DATABASE') or 'mysql://testdb:[email protected]:3306/db')
db.connect()
class Users(Model):
users_id = PrimaryKeyField()
username = CharField()
password = CharField()
mobile_number = CharField()
created_at = DateTimeField()
updated_at = DateTimeField()
class Meta:
database = db
class User_profiles(Model):
users_id = IntegerField()
user_profiles_id = PrimaryKeyField()
profile_name = CharField()
address = CharField()
created_at = DateTimeField()
updated_at = DateTimeField()
class Meta:
database = db
Users.create(username = "Adam", password = "Dummy1", mobile_number = "1234567891")
User_profiles.create(users_id=4,profile_name="shop", address="Delhi")
ユーザー& USERPROFILESがモデルです。これらのモデルを使って1つのファイルにエントリを作成することができます。
今、私は3つのファイルに分割しようとしています:main.py、users.py、userprofiles.py main.py - users.pyとuserprofiles.py
を呼び出す必要がありますメインファイルマイmain.py
import os
from peewee import *
from playhouse.db_url import connect
# Connect to the database URL defined in the environment, falling
# back to a local MySql database if no database URL is specified.
db = connect(os.environ.get('DATABASE') or 'mysql://testdb:[email protected]:3306/db')
db.connect()
Users.create(username = 'testname', password = '@[email protected]@', mobile_number='1234567811'):
マイusers.py:
import os
from peewee import *
from playhouse.db_url import connect
# Connect to the database URL defined in the environment, falling
# back to a local MySql database if no database URL is specified.
# db = connect(os.environ.get('DATABASE') or 'mysql://testdb:[email protected]:3306/db')
db.connect()
class Users(Model):
users_id = PrimaryKeyField()
username = CharField()
password = CharField()
mobile_number = CharField()
created_at = DateTimeField()
updated_at = DateTimeField()
class Meta:
database = db
マイuserprofiles.py:
import os
from peewee import *
from playhouse.db_url import connect
# Connect to the database URL defined in the environment, falling
# back to a local MySql database if no database URL is specified.
# db = connect(os.environ.get('DATABASE') or 'mysql://testdb:[email protected]:3306/db')
# db.connect()
class User_profiles(Model):
users_id = IntegerField()
user_profiles_id = PrimaryKeyField()
profile_name = CharField()
address = CharField()
created_at = DateTimeField()
updated_at = DateTimeField()
class Meta:
database = db
私はピーウィーを使用してmain.py内部のアクションを実行するためにmain.pyにusers.pyとuserprofiles.pyをインポートするにはどうすればよいですか?
上記のpyファイルをインポートし、2つのモデルをリンクしてdbアクションを実行しようとしています。 私はコーディングの初心者です。 Pythonの
users.pyとuserprofiles.pyの両方の内容が同じです。コピー/貼り付けの間違いだと思いますか?また、あなたは今までmain.pyで何を試しましたか? – zmo
@zmoそれは間違いだった。私は私の質問を再編集します。 – thepassionatecoder
[mcve]の作成方法を参照してください –