Skip to content

Instantly share code, notes, and snippets.

@suzumura-ss
Created November 12, 2015 08:45
Show Gist options
  • Save suzumura-ss/573a7261535c388cb85b to your computer and use it in GitHub Desktop.
Save suzumura-ss/573a7261535c388cb85b to your computer and use it in GitHub Desktop.
peewee with BIGINT primary key.
#from peewee import *
#import pymysql
import peewee
# 'id' to 'BIGINT AUTO_INCREMENT'
peewee.MySQLDatabase.field_overrides['primary_key'] = 'BIGINT AUTO_INCREMENT'
db = peewee.MySQLDatabase(
database='peewee_development',
user='root',
password='',
host='localhost',
port=3306)
class User(peewee.Model):
email = peewee.CharField(max_length=255)
password = peewee.CharField(max_length=255)
class Meta:
database = db
# http://stackoverflow.com/questions/21975920/peewee-model-to-json
def __str__(self):
r = {}
for k in self._data.keys():
try:
r[k] = str(getattr(self, k))
except:
r[k] = json.dumps(getattr(self, k))
return str(r)
db.drop_table(User)
db.create_tables([User], True)
User.create(email='foobar@example.com', password='very strong password')
for email in ['foobar@example.com', 'foobar@example2.com']:
try:
print User.get(User.email==email)
except User.DoesNotExist, e:
print "Record not found\n---\n{0}\n---".format(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment