2016-04-20 104 views
-1

我使用的是Windows上的Python 3.5和2.8.0 peewee'SqliteDatabase' 對象有沒有屬性 '_meta'

當我的代碼試圖運行:

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' 

自從peewee 2.10以後,這個錯誤沒有解決嗎?

您能否提供一種使用python 3打印元數據的方法?

謝謝。

Djalaboum

+0

你好像是以[文檔](http://docs.peewee-orm.com/en/latest/peewee/models.html#model-options-and-table-metadata)顯示的方式執行此操作,是否有可能會導致您的另一部分代碼安裝不正確? –

+0

wait ...不,SQLDatabase沒有模型的'._meta',你需要提供[MCVE](http://stackoverflow.com/help/mcve)。 –

回答

-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.

0

不能調用數據庫對象 「sorted_fields」 - 這僅適用於一個Model類

回溯告訴你數據庫對象沒有「_meta」,這是正確的。只有模型可以。

如果你想你的反思數據庫,使用:

  • database.get_tables()
  • database.get_columns(表)
  • database.get_primary_keys(表)
  • database.get_foreign_keys (表)
  • database.get_indexes(table)
+0

謝謝,它幫助了我。我正在嘗試瞭解如何使用在類Pet中定義的ForeignKeyField ...祝你有個美好的一天:-) – Djalaboum

相關問題