簡體   English   中英

'SqliteDatabase' 對象沒有屬性 '_meta'

[英]'SqliteDatabase' object has no attribute '_meta'

我在 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'  

請問這個錯誤自peewee 2.10以來沒有解決嗎?

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

謝謝。

賈拉布姆

您不能在數據庫對象上調用“sorted_fields”——這僅適用於Model 類

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

如果要內省數據庫,請使用:

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

感謝您回復 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM