How to use the peewee.ForeignKeyField function in peewee

To help you get started, we’ve selected a few peewee examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github daeyun / object-shapes-cvpr18 / python / mvshape / db_model.py View on Github external
database = db


class Example(BaseModel):
    pass


class Tag(BaseModel):
    name = peewee.CharField(unique=True, max_length=128, index=True)
    description = peewee.TextField(null=True)


class ExampleTag(BaseModel):
    """ Many-to-many. """
    example = peewee.ForeignKeyField(Example)
    tag = peewee.ForeignKeyField(Tag)


class Dataset(BaseModel):
    name = peewee.CharField(unique=True, max_length=128, index=True)
    description = peewee.TextField(null=True)


class ExampleDataset(BaseModel):
    """ Many-to-many. """
    example = peewee.ForeignKeyField(Example)
    dataset = peewee.ForeignKeyField(Dataset)


class Split(BaseModel):
    """
    Train, test, validation, validation_subset, etc..
github b1naryth1ef / bard / bard / models / episode.py View on Github external
@BaseModel.register
class Episode(BaseModel):
    class Meta:
        indexes = ((("season", "number"), True),)

    class State:
        NONE = 0
        WANTED = 1
        FETCHED = 2
        DOWNLOADED = 3

        ALL = {NONE, WANTED, FETCHED, DOWNLOADED}

    season = ForeignKeyField(Season, backref="episodes", on_delete="CASCADE")
    state = IntegerField(default=State.NONE, choices=State.ALL)

    number = CharField()
    name = CharField(null=True)
    desc = CharField(null=True)
    airdate = DateTimeField(null=True)

    # Metadata
    imdb_id = CharField(null=True)

    # Quality Preference
    quality = CharField(default="", null=True)

    @property
    def aired(self):
        if self.airdate:
github anqxyr / pyscp / pyscp / orm.py View on Github external
class ForumPost(BaseModel):
    thread = peewee.ForeignKeyField(
        ForumThread, related_name='posts', index=True)
    user = peewee.ForeignKeyField(User, related_name='posts', index=True)
    parent = peewee.ForeignKeyField('self', null=True)
    title = peewee.CharField(null=True)
    time = peewee.DateTimeField()
    content = peewee.TextField()


class Tag(BaseModel):
    name = peewee.CharField(unique=True)


class PageTag(BaseModel):
    page = peewee.ForeignKeyField(Page, related_name='tags', index=True)
    tag = peewee.ForeignKeyField(Tag, related_name='pages', index=True)


class OverrideType(BaseModel):
    name = peewee.CharField(unique=True)


class Override(BaseModel):
    url = peewee.ForeignKeyField(Page, to_field=Page.url, index=True)
    user = peewee.ForeignKeyField(User, index=True)
    type = peewee.ForeignKeyField(OverrideType)


class ImageStatus(BaseModel):
    name = peewee.CharField(unique=True)
github paurieraf / musicbucket-bot / src / bot / models.py View on Github external
class Track(BaseModel, EmojiModelMixin):
    EMOJI = ':musical_note:'

    id = CharField(primary_key=True)
    name = CharField()
    track_number = IntegerField(null=True)
    duration_ms = IntegerField(null=True)
    explicit = BooleanField(null=True)
    popularity = IntegerField(null=True)
    href = CharField(null=True)
    spotify_url = CharField(null=True)
    preview_url = CharField(null=True)
    uri = CharField()
    album = ForeignKeyField(Album, backref='tracks')
    artists = ManyToManyField(Artist, backref='tracks')

    def __str__(self):
        return self.name

    def get_first_artist(self):
        if self.artists:
            return self.artists.first()
        return None

    @classmethod
    def get_emoji(cls):
        return emojize(cls.EMOJI, use_aliases=True)


TrackArtist = Track.artists.get_through_model()
github Terrance / IMMP / immp / hook / identitylocal.py View on Github external
return "<{}: #{} {} @ {} {}>".format(self.__class__.__name__, self.id, repr(self.user),
                                             repr(self.network), repr(self.group))


class IdentityRole(BaseModel):
    """
    Assignment of a role to an identity.

    Attributes:
        group (.IdentityGroup):
            Containing group instance.
        role (str):
            Plain role identifier.
    """

    group = ForeignKeyField(IdentityGroup, backref="roles", on_delete="cascade")
    role = CharField()

    def __repr__(self):
        return "<{}: #{} {} {}>".format(self.__class__.__name__, self.id, repr(self.role),
                                        repr(self.group))


class LocalIdentityHook(immp.Hook, AccessPredicate, IdentityProvider):
    """
    Hook for managing physical users with multiple logical links across different plugs.  This
    effectively provides self-service identities, as opposed to being provided externally.
    """

    schema = immp.Schema({immp.Optional("instance"): immp.Nullable(int),
                          "plugs": [str],
                          immp.Optional("multiple", True): bool})
github CityOfZion / neo-python / neo / Implementations / Wallets / peewee / Models.py View on Github external
TxId = BlobField()
    Index = IntegerField()
    AssetId = BlobField()
    Value = IntegerField()
    ScriptHash = BlobField()
    State = IntegerField()
    Address = ForeignKeyField(Address)


class Contract(ModelBase):
    Id = AutoField()
    RawData = CharField()
    ScriptHash = BlobField()
    PublicKeyHash = CharField()
    Account = ForeignKeyField(Account, null=True)
    Address = ForeignKeyField(Address)


class Key(ModelBase):
    Id = AutoField()
    Name = CharField(unique=True)
    Value = BlobField()


class NEP5Token(ModelBase):
    ContractHash = CharField(unique=True)
    Name = CharField()
    Symbol = CharField()
    Decimals = IntegerField()


class Transaction(ModelBase):
github Polsaker / throat / migrations / 001_initial.py View on Github external
parentcid = pw.ForeignKeyField(backref='subpostcomment_set', column_name='parentcid', field='cid', model='self', null=True)
        pid = pw.ForeignKeyField(backref='subpostcomment_set', column_name='pid', field='pid', model=migrator.orm['sub_post'], null=True)
        score = pw.IntegerField(null=True)
        upvotes = pw.IntegerField(constraints=[SQL("DEFAULT 0")])
        downvotes = pw.IntegerField(constraints=[SQL("DEFAULT 0")])
        status = pw.IntegerField(null=True)
        time = pw.DateTimeField(null=True)
        uid = pw.ForeignKeyField(backref='comments', column_name='uid', field='uid', model=migrator.orm['user'], null=True)

        class Meta:
            table_name = "sub_post_comment"

    @migrator.create_model
    class SubPostCommentReport(pw.Model):
        id = pw.AutoField()
        cid = pw.ForeignKeyField(backref='subpostcommentreport_set', column_name='cid', field='cid', model=migrator.orm['sub_post_comment'])
        uid = pw.ForeignKeyField(backref='subpostcommentreport_set', column_name='uid', field='uid', model=migrator.orm['user'])
        datetime = pw.DateTimeField()
        reason = pw.CharField(max_length=128)

        class Meta:
            table_name = "sub_post_comment_report"

    @migrator.create_model
    class SubPostCommentVote(pw.Model):
        xid = pw.PrimaryKeyField()
        datetime = pw.DateTimeField(null=True)
        cid = pw.CharField(max_length=255, null=True)
        positive = pw.IntegerField(null=True)
        uid = pw.ForeignKeyField(backref='subpostcommentvote_set', column_name='uid', field='uid', model=migrator.orm['user'], null=True)

        class Meta:
github Polsaker / throat / app / models.py View on Github external
sid = ForeignKeyField(db_column='sid', null=True, model=Sub, field='sid')
    uid = ForeignKeyField(db_column='uid', null=True, model=User, field='uid')
    target = ForeignKeyField(db_column='target_uid', null=True, model=User, field='uid')
    admin = BooleanField(default=False)  # True if action was performed by an admin override.
    time = DateTimeField(default=datetime.datetime.utcnow)

    def __repr__(self):
        return f''

    class Meta:
        table_name = 'sub_log'


class SubMetadata(BaseModel):
    key = CharField(null=True)
    sid = ForeignKeyField(db_column='sid', null=True, model=Sub,
                          field='sid')
    value = CharField(null=True)
    xid = PrimaryKeyField()

    def __repr__(self):
        return f''

    class Meta:
        table_name = 'sub_metadata'


class SubPost(BaseModel):
    content = TextField(null=True)
    deleted = IntegerField(null=True) # 1=self delete, 2=mod delete, 0=not deleted
    link = CharField(null=True)
    nsfw = BooleanField(null=True)
github Budovi / ddmbot / database / common.py View on Github external
# we will need this to resolve a foreign key loop
DeferredUser = peewee.DeferredRelation()
DeferredLink = peewee.DeferredRelation()


# Table for storing playlists, as many as user wants
class Playlist(DdmBotSchema):
    id = peewee.PrimaryKeyField()

    # playlist is owned by a user
    user = peewee.ForeignKeyField(DeferredUser)
    # for an identifier, we choose a "nice enough" name
    name = peewee.CharField()
    # the first song of the playlist
    head = peewee.ForeignKeyField(DeferredLink, null=True, default=None)
    # playlist may be set to repeat itself, this is default except to implicit one
    repeat = peewee.BooleanField(default=True)

    class Meta:
        # we want the couple (user, name) to be unique (so no user has two playlists with the same name)
        constraints = [peewee.SQL('UNIQUE(user_id, name)')]


# Table for storing songs in playlist -- linked list approach
class Link(DdmBotSchema):
    id = peewee.PrimaryKeyField()

    playlist = peewee.ForeignKeyField(Playlist)
    song = peewee.ForeignKeyField(Song)
    next = peewee.ForeignKeyField('self', null=True)
github Polsaker / throat / app / models.py View on Github external
field='pid')
    uid = ForeignKeyField(db_column='uid', null=True, model=User,
                          field='uid')
    fileid = CharField(null=True)
    thumbnail = CharField(null=True)
    status = IntegerField()

    def __repr__(self):
        return f''

    class Meta:
        table_name = 'user_uploads'


class SubUploads(BaseModel):
    sid = ForeignKeyField(db_column='sid', model=Sub, field='sid')
    fileid = CharField()
    thumbnail = CharField()
    name = CharField()
    size = IntegerField()

    def __repr__(self):
        return f''

    class Meta:
        table_name = 'sub_uploads'


class SubPostReport(BaseModel):
    pid = ForeignKeyField(db_column='pid', model=SubPost, field='pid')
    uid = ForeignKeyField(db_column='uid', model=User, field='uid')
    datetime = DateTimeField(default=datetime.datetime.now)