简体   繁体   English

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

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

I'm using on Windows python 3.5 and peewee 2.8.0我在 Windows python 3.5 和 peewee 2.8.0 上使用

When trying in my code to run:尝试在我的代码中运行时:

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

This is my output:这是我的输出:

# 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'  

Is this error not solved since peewee 2.10 please?请问这个错误自peewee 2.10以来没有解决吗?

Could you please provide a way to print metadata using python 3?您能否提供一种使用python 3打印元数据的方法?

Thanks.谢谢。

Djalaboum贾拉布姆

You cannot call "sorted_fields" on the database object -- this only pertains to a Model class .您不能在数据库对象上调用“sorted_fields”——这仅适用于Model 类

The traceback is telling you that database objects have no "_meta", which is correct.回溯告诉您数据库对象没有“_meta”,这是正确的。 Only Models do.只有模型可以。

If you want to introspect your database, use the:如果要内省数据库,请使用:

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

Thanks for replying Tadhg.感谢您回复 Tadhg。

Hereunder my complete code:下面是我的完整代码:

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)

If you need more info please let me know.如果您需要更多信息,请告诉我。

Thanks for your help.谢谢你的帮助。

D. D.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;Request&#39;对象没有属性&#39;META&#39; - 'Request' object has no attribute 'META' &#39;AnonymousUser&#39; 对象没有属性 &#39;_meta&#39; | Django 认证 - 'AnonymousUser' object has no attribute '_meta' | Django Authentication 如何解决“ dict”对象没有属性“ _meta” - how to solve the 'dict' object has no attribute '_meta' 出现错误“ QuerySet”对象没有属性“ _meta” - getting error 'QuerySet' object has no attribute '_meta' Djnago 'ImageFieldFile' object 没有属性 '_meta' - Djnago 'ImageFieldFile' object has no attribute '_meta' Django:FileField&#39; 对象在类 Meta 中没有属性 &#39;attrs&#39; - Django: FileField' object has no attribute 'attrs' in class Meta Django 中的迁移错误:AttributeError: 'str' object 没有属性 '_meta' - Migrations Error in Django: AttributeError: 'str' object has no attribute '_meta' Django1.9:&#39;function&#39; 对象没有属性 &#39;_meta&#39; - Django1.9: 'function' object has no attribute '_meta' Django Python3-AttributeError:“模块”对象没有属性“ META” - Django Python3 - AttributeError: 'module' object has no attribute 'META' Django rest-api - 属性错误:'str' object 没有属性 '_meta' - Django rest-api - attributeerror: 'str' object has no attribute '_meta'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM