0

I came across an example when searching for sample python code:

import peewee
from peewee import *

db = MySQLDatabase('jonhydb', user='john',passwd='megajonhy')

class Book(peewee.Model):
    author = peewee.CharField()
    title = peewee.TextField()

    class Meta:
        database = db

I can understand up to title = peewee.TextField(), but I'm a little confused with line class Meta: .... Why is that needed? Isn't there another way to establish a connection?

James Lim
  • 12,915
  • 4
  • 40
  • 65
James Hallen
  • 4,534
  • 4
  • 23
  • 28
  • This [related question](http://stackoverflow.com/questions/719705/what-is-the-purpose-of-pythons-inner-classes) might be useful. – James Lim Jul 10 '13 at 12:11

1 Answers1

3

That is the Peewee ORM's way of specifying which database connection model classes should use. It's specified in a subclass so that Peewee doesn't treat it as a model field. More detailed information is available in the Peewee cookbook article covering Database and Connection recipes.

Kaivosukeltaja
  • 15,541
  • 4
  • 40
  • 70
  • Yep -- it is a way of storing and inheriting "metadata" about a model. Peewee uses metaclasses to do some interesting magic with ``Meta``. If you're interested take a peek at the source. – coleifer Jul 10 '13 at 13:13
  • For some reason there is no Cookbook in http://peewee.readthedocs.org/en/latest/ anymore, so the link above isn't working.. [The last Cookbook I could find ist this (from v 2.0.2)](https://peewee.readthedocs.org/en/2.0.2/peewee/cookbook.html). As I write this v2.7.4 is latest. – klaas Dec 15 '15 at 14:44