How do I associate user_id in UserImg with User table img_id with img table so that pycharm has a hint #1790
Closed
2622594863
started this conversation in
General
Replies: 1 comment
-
One solution is ForeignKeyRelation: from typing import Optional
class UserImg(Model):
id = fields.IntField(pk=True)
user: Optional[fields.ForeignKeyRelation[User]] = fields.ForeignKeyField(
"models.User", null=True, on_delete=fields.OnDelete.SET_NULL
)
img: Optional[fields.ForeignKeyRelation[Img]] = fields.ForeignKeyField(
"models.Img", null=True, on_delete=fields.OnDelete.SET_NULL
)
time = fields.DatetimeField(null=True)
note_1 = fields.CharField(null=True, max_length=900)
note_2 = fields.CharField(null=True, max_length=900)
note_3 = fields.CharField(null=True, max_length=900)
user_id: int
img_id: int
class Meta:
table = "qq_img"
def show(pk: int) -> None:
print(pk)
async def main() -> None:
user_img = await UserImg.filter(id=1).prefetch_related("user", "img").get()
if user_img.user_id >= 1 and user_img.user:
show(user_img.img_id)
user: User = user_img.user
print(user.email)
img: Img = user_img.img
print(img.img_url) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How do I associate user_id in UserImg with User table img_id with img table so that pycharm has a hint
from tortoise import Model, fields
class User(Model):
id = fields.IntField(pk=True)
qq = fields.CharField(null=True, max_length=15, unique=True)
password = fields.CharField(null=True, max_length=20)
email = fields.CharField(null=True, max_length=900)
note_1 = fields.CharField(null=True, max_length=900)
note_2 = fields.CharField(null=True, max_length=900)
note_3 = fields.CharField(null=True, max_length=900)
events: fields.ReverseRelation["QqImg"]
class Img(Model):
id = fields.IntField(pk=True)
img_type = fields.CharField(null=True, max_length=900, unique=True)
img_name = fields.CharField(null=True, max_length=900, unique=True)
thumbnail_name = fields.CharField(null=True, max_length=900)
thumbnail = fields.CharField(null=True, max_length=900)
img_url = fields.CharField(null=True, max_length=900, unique=True)
author = fields.CharField(null=True, max_length=900)
note_1 = fields.CharField(null=True, max_length=900)
note_2 = fields.CharField(null=True, max_length=900)
note_3 = fields.CharField(null=True, max_length=900)
class UserImg(Model):
id = fields.IntField(pk=True)
user_id = fields.IntField(null=True, index=True)#This corresponds to user
img_id = fields.IntField(null=True, index=True)#This corresponds to Img
time = fields.DatetimeField(null=True)
note_1 = fields.CharField(null=True, max_length=900)
note_2 = fields.CharField(null=True, max_length=900)
note_3 = fields.CharField(null=True, max_length=900)
Beta Was this translation helpful? Give feedback.
All reactions