Peewee KeyError:'我'

时间:2015-06-16 11:17:28

标签: python flask peewee

我从Python的peewee模块得到一个奇怪的错误,我无法解决,任何想法?我基本上希望拥有包含多个公司的“批次”。我正在为每个批次创建一个批处理实例,并将其中的所有公司分配给该批次的行ID。

回溯

Traceback (most recent call last):
  File "app.py", line 16, in <module>
    import models
  File "/Users/wyssuser/Desktop/dscraper/models.py", line 10, in <module>
    class Batch(Model):
  File "/Library/Python/2.7/site-packages/peewee.py", line 3647, in __new__
    cls._meta.prepared()
  File "/Library/Python/2.7/site-packages/peewee.py", line 3497, in prepared
    field = self.fields[item.lstrip('-')]
KeyError: 'i'

models.py

from datetime import datetime

from flask.ext.bcrypt import generate_password_hash
from flask.ext.login import UserMixin

from peewee import *

DATABASE = SqliteDatabase('engineering.db')

class Batch(Model):
  initial_contact_date = DateTimeField(formats="%m-%d-%Y")

  class Meta:
    database = DATABASE
    order_by = ('initial_contact_date')

  @classmethod
  def create_batch(cls, initial_contact_date):
    try:
      with DATABASE.transaction():
        cls.create(
          initial_contact_date=datetime.now
          )
        print 'Created batch!'
    except IntegrityError:
      print 'Whoops, there was an error!'


class Company(Model):
  batch_id = ForeignKeyField(rel_model=Batch, related_name='companies')
  company_name = CharField()
  website = CharField(unique=True)
  email_address = CharField()
  scraped_on = DateTimeField(formats="%m-%d-%Y")
  have_contacted = BooleanField(default=False)
  current_pipeline_phase = IntegerField(default=0)

  day_0_message_id = IntegerField()
  day_0_response = IntegerField()
  day_0_sent = DateTimeField()

  day_5_message_id = IntegerField()
  day_5_response = IntegerField()
  day_5_sent = DateTimeField()

  day_35_message_id = IntegerField()
  day_35_response = IntegerField()
  day_35_sent = DateTimeField()

  day_125_message_id = IntegerField()
  day_125_response = IntegerField()
  day_125_sent = DateTimeField()

  sector = CharField()

  class Meta:
    database = DATABASE
    order_by = ('have_contacted', 'current_pipeline_phase')

  @classmethod
  def create_company(cls, company_name, website, email_address):
    try:
      with DATABASE.transaction():
        cls.create(company_name=company_name, website=website, email_address=email_address, scraped_on=datetime.now)
        print 'Saved {}'.format(company_name)
    except IntegrityError:
      print '{} already exists in the database'.format(company_name)


def initialize():
  DATABASE.connect()
  DATABASE.create_tables([Batch, Company, User],safe=True)
  DATABASE.close()

1 个答案:

答案 0 :(得分:9)

问题在于您的Batch类的元数据。请参阅使用order_by的peewee example

class User(BaseModel):
    username = CharField(unique=True)
    password = CharField()
    email = CharField()
    join_date = DateTimeField()

    class Meta:
        order_by = ('username',)

其中order_by是仅包含用户名的元组。在您的示例中,您省略了逗号,使其成为常规字符串而不是元组。这将是您的代码部分的正确版本:

class Batch(Model):
    initial_contact_date = DateTimeField(formats="%m-%d-%Y")

    class Meta:
        database = DATABASE
        order_by = ('initial_contact_date',)