class attribute

Chris Angelico rosuav at gmail.com
Thu Jan 28 09:56:40 EST 2016


On Fri, Jan 29, 2016 at 12:56 AM, jmp <jeanmichel at sequans.com> wrote:
> My guess is that models.Model has a metclass. Without going too much int
> details, the metaclass may change the class structure when it's created.
>
> django is very specific and very database oriented.
>
> "
> article = Article(titre="Bonjour", auteur="Maxime")
> article.contenu = "Les crêpes bretonnes sont trop bonnes !"
> "
>
> this is probably the wrong way to assign a value to 'contenu'. You should
> have a look at django help files, from what I remember it's very well
> documented with a lot of examples.
>

I suspect Django's using the descriptor protocol or other magic here.
SQLAlchemy works in a similar way; when you create the class, you put
a bunch of attributes on it to specify columns, and then you can
simply assign to those attributes on an instance to set the values for
a row to be saved to the database.

As Todd explains in further detail, the metaclass gets to do whatever it likes.

ChrisA



More information about the Python-list mailing list