' SqliteDatabase'对象没有属性' _meta'

时间:2016-04-20 15:15:11

标签: python-3.x peewee

我在Windows上使用python 3.5和peewee 2.8.0

尝试运行我的代码时:

for field in db._meta.sorted_field_names:
    print(field.name)

这是我的输出:

# py -3 .\basic_example.py
Info about Grandma L. using SelectQuery: Grandma L.
Grandma L. info using Model.get: Friday 01 March 1935
Traceback (most recent call last):
  File ".\basic_example.py", line 86, in <module>
    for field in db._meta.sorted_field_names:
AttributeError: 'SqliteDatabase' object has no attribute '_meta'  

自从小便2.10起,这个错误没有解决吗?

您能否提供一种使用python 3打印元数据的方法?

感谢。

Djalaboum

2 个答案:

答案 0 :(得分:0)

您无法在数据库对象上调用“sorted_fields” - 这仅适用于 Model类

回溯告诉您数据库对象没有“_meta”,这是正确的。只有模特才能做到。

如果您想内省数据库,请使用:

  • database.get_tables()
  • database.get_columns(表)
  • database.get_primary_keys(表)
  • database.get_foreign_keys(表)
  • database.get_indexes(表)

答案 1 :(得分:-1)

感谢您回复Tadhg。

下面是我的完整代码:

from peewee import *
from datetime import date

db = SqliteDatabase('people.db')

class Person(Model):
name = CharField()             # varchar in sqlite
birthday = DateField()         # date in sqlite
is_relative = BooleanField()   # integer in sqlite

class Meta:
    database = db # This model uses the "people.db" database.

class Pet(Model):
    owner = ForeignKeyField(Person, related_name='pets')
    name = CharField()
    animal_type = CharField()

class Meta:
    database = db # this model uses the "people.db" database

db.connect()
db.create_tables([Person, Pet],True) # Parameters:  fail_silently (bool) –  If set to True, the method will check for the existence of the table before attempting to create.

# save() method: when you call save(), the number of rows modified is returned.                
uncle_bob = Person(name='Bob', birthday=date(1960, 1, 15), is_relative=True)
uncle_bob.save() # bob is now stored in the database

# create() method: which returns a model instance
grandma = Person.create(name='Grandma', birthday=date(1935, 3, 1), is_relative=True)
herb = Person.create(name='Herb', birthday=date(1950, 5, 5), is_relative=False)                

# To update a row, modify the model instance and call save() to persist the changes

grandma.name = 'Grandma L.'
grandma.save()  # Update grandma's name in the database.

# Let’s give them some pets.

bob_kitty = Pet.create(owner=uncle_bob, name='Kitty', animal_type='cat')
herb_fido = Pet.create(owner=herb, name='Fido', animal_type='dog')
herb_mittens = Pet.create(owner=herb, name='Mittens', animal_type='cat')
herb_mittens_jr = Pet.create(owner=herb, name='Mittens Jr', animal_type='cat')

# Mittens sickens and dies. We need to remove him from the database: the return value of delete_instance() is the number of rows removed from the database.

herb_mittens.delete_instance() # he had a great life

# Uncle Bob decides that too many animals have been dying at Herb’s house, so he adopts Fido:

herb_fido.owner = uncle_bob
herb_fido.save()
bob_fido = herb_fido # rename our variable for clarity

## Getting single records

# To get a single record from the database, use SelectQuery.get():
grandma = Person.select().where(Person.name == 'Grandma L.').get()

print("Info about Grandma L. using SelectQuery: %s "% grandma.name)

# Or using the equivalent shorthand Model.get():

grandma = Person.get(Person.name == 'Grandma L.')

display_bday=grandma.birthday.strftime("%A %d %B %Y")

print("Grandma L. info using Model.get: %s "% display_bday)

for field in db._meta.sorted_field_names:
    print(field.name)

如果您需要更多信息,请告诉我。

感谢您的帮助。

d