-1

I'm using on Windows python 3.5 and 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?

Could you please provide a way to print metadata using python 3?

Thanks.

Djalaboum

Djalaboum
  • 1
  • 1
  • you seem to be doing it the way the [documentation](http://docs.peewee-orm.com/en/latest/peewee/models.html#model-options-and-table-metadata) shows, is it possible another part of your code is not setup correctly? – Tadhg McDonald-Jensen Apr 20 '16 at 15:20
  • wait... no the SQLDatabase doesn't have a `._meta` the model does, you need to provide a [MCVE](http://stackoverflow.com/help/mcve). – Tadhg McDonald-Jensen Apr 20 '16 at 15:26

2 Answers2

1

You cannot call "sorted_fields" on the database object -- this only pertains to a Model class.

The traceback is telling you that database objects have no "_meta", which is correct. Only Models do.

If you want to introspect your database, use the:

  • database.get_tables()
  • database.get_columns(table)
  • database.get_primary_keys(table)
  • database.get_foreign_keys(table)
  • database.get_indexes(table)
coleifer
  • 24,887
  • 6
  • 60
  • 75
  • thanks, it helped me. I'm trying now to understand how to use ForeignKeyField defined in class Pet ... Have a nice day :-) – Djalaboum Apr 25 '16 at 13:44
-1

Thanks for replying 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.

Djalaboum
  • 1
  • 1