Replies: 2 comments
-
|
@Fidel-C There is currently no import asyncio
import datetime
from piccolo.apps.user.tables import BaseUser
from piccolo.columns import ForeignKey, Numeric, Timestamp, Varchar
from piccolo.table import Table
class Profile(Table):
user = ForeignKey(BaseUser, null=True)
phone = Varchar()
wallet = Numeric()
created_at = Timestamp()
updated_at = Timestamp(auto_update=datetime.datetime.now)
async def link_user_to_profile():
# create user
user_bob = await BaseUser.create_user(
username="bob",
email="[email protected]",
password="abc123",
active=True,
)
# save created user profile
bob_profile = Profile(
phone="123456789",
user=user_bob,
wallet=100,
)
await bob_profile.save()
async def main():
# Tables creating
await BaseUser.create_table(if_not_exists=True)
await Profile.create_table(if_not_exists=True)
if not await BaseUser.exists().where(BaseUser.email == "[email protected]"):
await Profile.link_user_to_profile()
if __name__ == "__main__":
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Thanks a lot. I've a similar functions to this in my code. Which works with the sign-up endpoint. Was looking for the one that would also work when I run create user from cli. |
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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am wondering whether it is possible to also pass a different Table as a hook dependency apart from the Request object.
eg. where there's a foreign key relationship between the BaseUser and a Profile table.
something like this
` from piccolo.table import Table
from piccolo import columns
from piccolo_api.crud.endpoints import BaseUser
import datetime
from .custom_auth import get_current_user
class Profile(Table):
user=columns.ForeignKey(BaseUser,on_delete=columns.OnDelete.cascade,null=True)
phone=columns.Varchar()
wallet=columns.Numeric()
created_at=columns.Timestamp(datetime.datetime.now)
updated_at=columns.Timestamp(auto_update=datetime.datetime.now)
async def link_user_to_profile(user:BaseUser, profile: Profile))):
profile.user=user
profile.wallet=100
return user `
The expectation is that a new profile is created whenever BaseUser is saved.
In my app I wrote the logic to link a new user to a new profile in the register method, but it does not work with "piccolo create user" command.
Beta Was this translation helpful? Give feedback.
All reactions