adamczi adamczi - 4 months ago 8
Python Question

create_or_get with data from user input

Trying to implement a user registration form on a website I can't make user input to be added as a new user to the database. I'm using Flask with Peewee, and have:

nUser ={
'email': request.form['e_mail'],
'uname': request.form['username'],
'pword': request.form['password'],
'time': strftime('%Y-%m-%d %H:%M:%S')

}
nUser = User.create_or_get()


where nUser is temporary dict for new user. I should mention, that with the official peewee docs I tried
nUser, created = User.create_or_get(nUser)
what returned an error of too many arguments.

A code about
User
class is below (stored in separate .py, but there is
from models import *
up there):

class User(Model):
email=TextField()
uname=TextField(unique=True)
pword=TextField()
time=TextField()

class Meta:
database = db


Although it looks like it "works" from a webuser side, no record is added to the database. Previously I did it with
User.select().where(User.uname......
and it worked, but now I would like to switch to
create_or_get()
method since it would shorten my code and looks more efficient. Logging in for old users works, so the connection and access to the DB is fine. I would appreciate any hints.

UPDATE

Thanks to @Daniel Roseman the final query would be (if anyone in the future will need it):

nUser, created = User.create_or_get(**nUser)

Answer

The official documentation that you link to show that create_or_get takes keyword arguments only. To pass a dict, you need to use the ** syntax:

User.create_or_get(**nuser)
Comments