-1

クラス:

class User(flask_db.Model, UserMixin):
    id = PrimaryKeyField()
    social_id = CharField(null=False, unique=True)
    nickname = CharField(null=False)
    email = CharField(null=True)

class Feed(flask_db.Model):
    id = PrimaryKeyField()
    user = ForeignKeyField(User, related_name='feeds')
    title = CharField()
    url = CharField()
    description = CharField()

私が次のようなことをした場合:

newfeed = Feed.create(..)

type id と type user には何を入力すればよいですか?

User のすべてのフィードを取得するにはどうすればよいですか? このようなもので?

    feedsofuserTom = Feed.select().where(Feed.user == userTom)

私は ForeignKeyField() 関数を理解していません。ドキュメントを読みましたが、例がうまくいきませんでした。 http://docs.peewee-orm.com/en/latest/peewee/models.html

4

1 に答える 1

1

ForeignKey はモデル間の関係です。ForeignKey は ManyToOne 関係です。したがって、フィードには、必要な数のユーザーを関連付けることができます。

あなたの例では、まずユーザーインスタンスを作成する必要があります。次に、フィードの作成中に追加します。first_user = User.create(social_id=1、ニックネーム=名前、email="email") newfeed = Feed.create(user=first_user、title="クールなフィード"、url="url.com"、description="description" )

私が覚えている限り、PrimaryKeyField() を追加する必要はありません。このフィールドは、データベースによって自動的に追加されます。

于 2016-05-21T22:50:23.193 に答える