Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102180 > unrolled thread
| Started by | "ast" <nomail@invalid.com> |
|---|---|
| First post | 2016-01-28 14:15 +0100 |
| Last post | 2016-01-28 11:04 -0500 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
class attribute "ast" <nomail@invalid.com> - 2016-01-28 14:15 +0100
Re: class attribute jmp <jeanmichel@sequans.com> - 2016-01-28 14:56 +0100
Re: class attribute Todd Dembrey <todd.dembrey@gmail.com> - 2016-01-28 14:13 +0000
Re: class attribute "ast" <nomail@invalid.com> - 2016-01-28 15:24 +0100
Re: class attribute Chris Angelico <rosuav@gmail.com> - 2016-01-29 01:56 +1100
Re: class attribute Joel Goldstick <joel.goldstick@gmail.com> - 2016-01-28 11:04 -0500
| From | "ast" <nomail@invalid.com> |
|---|---|
| Date | 2016-01-28 14:15 +0100 |
| Subject | class attribute |
| Message-ID | <56aa1474$0$27833$426a74cc@news.free.fr> |
hello
Here is a class from django framework
from django.db import models
class Article(models.Model):
titre = models.CharField(max_length=100)
auteur = models.CharField(max_length=42)
contenu = models.TextField(null=True)
date = models.DateTimeField(auto_now_add=True, auto_now=False,
verbose_name="Date de parution")
def __str__(self):
return self.titre
From a Python point of view, what are titre, auteur, contenu and date ?
Are they class attributes, so common to all instance of Article ?
It seems so to me.
But if i do in a django shell (run with py manage.py shell)
>>> Article.titre
it doesnt work,
AttributeError: type object 'Article' has no attribute 'titre'
why ?
if I test on a small class
>>>class MyClass:
>>> i=0
>>>
>>> MyClass.i
>>> 0
works
When we create an object of class Article
article = Article(titre="Bonjour", auteur="Maxime")
article.contenu = "Les crêpes bretonnes sont trop bonnes !"
we use the same names titre, auteur, contenu, which should be instance
attribute this time. This is confusing to me
thx
[toc] | [next] | [standalone]
| From | jmp <jeanmichel@sequans.com> |
|---|---|
| Date | 2016-01-28 14:56 +0100 |
| Message-ID | <mailman.54.1453989841.2338.python-list@python.org> |
| In reply to | #102180 |
On 01/28/2016 02:15 PM, ast wrote: > hello > > Here is a class from django framework > > > from django.db import models > > class Article(models.Model): > > titre = models.CharField(max_length=100) > auteur = models.CharField(max_length=42) > contenu = models.TextField(null=True) > date = models.DateTimeField(auto_now_add=True, auto_now=False, > verbose_name="Date de parution") > > def __str__(self): > return self.titre > > From a Python point of view, what are titre, auteur, contenu and date ? > Are they class attributes, so common to all instance of Article ? > It seems so to me. > > But if i do in a django shell (run with py manage.py shell) > >>>> Article.titre > > it doesnt work, > AttributeError: type object 'Article' has no attribute 'titre' > why ? > > if I test on a small class > >>>> class MyClass: >>>> i=0 >>>> >>>> MyClass.i >>>> 0 > > works > > > > When we create an object of class Article > > article = Article(titre="Bonjour", auteur="Maxime") > article.contenu = "Les crêpes bretonnes sont trop bonnes !" > > we use the same names titre, auteur, contenu, which should be instance > attribute this time. This is confusing to me > > thx 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. jm
[toc] | [prev] | [next] | [standalone]
| From | Todd Dembrey <todd.dembrey@gmail.com> |
|---|---|
| Date | 2016-01-28 14:13 +0000 |
| Message-ID | <mailman.55.1453990443.2338.python-list@python.org> |
| In reply to | #102180 |
On 28/01/16 13:15, ast wrote: > hello > > Here is a class from django framework > > > from django.db import models > > class Article(models.Model): > > titre = models.CharField(max_length=100) > auteur = models.CharField(max_length=42) > contenu = models.TextField(null=True) > date = models.DateTimeField(auto_now_add=True, auto_now=False, > verbose_name="Date de parution") > > def __str__(self): > return self.titre > > From a Python point of view, what are titre, auteur, contenu and date ? > Are they class attributes, so common to all instance of Article ? > It seems so to me. > > But if i do in a django shell (run with py manage.py shell) > >>>> Article.titre > > it doesnt work, > AttributeError: type object 'Article' has no attribute 'titre' > why ? > > if I test on a small class > >>>> class MyClass: >>>> i=0 >>>> >>>> MyClass.i >>>> 0 > > works > > > > When we create an object of class Article > > article = Article(titre="Bonjour", auteur="Maxime") > article.contenu = "Les crêpes bretonnes sont trop bonnes !" > > we use the same names titre, auteur, contenu, which should be instance > attribute this time. This is confusing to me > > thx Django Model classes are very different from a basic Python classes. It is worth having a look inside django/db/models/base.py for more information. But in summary, see below. The Model class has a metaclass of ModelBase which on __new__() returns a class constructed from the attributes which you have defined in your Model class (Article in this example). All the information is retained, it is just filed away neatly for other purposes. Have a look in the Article._meta attribute. You can find the fields you provided through Article._meta.fields. When you create Article (as above) you run the __init__() which reaches into the ._meta attribute of the class in order to create an object, which is then stored within the 'database'. This is done because you don't want to access the attributes on the Model, so they are removed when the Model is created. What you actually want to do is access them on the objects within the Model. Writing Article.objects.first().titre in Django Models is mostly equivalent to Article.titre from Python classes. Todd
[toc] | [prev] | [next] | [standalone]
| From | "ast" <nomail@invalid.com> |
|---|---|
| Date | 2016-01-28 15:24 +0100 |
| Message-ID | <56aa24c2$0$9249$426a74cc@news.free.fr> |
| In reply to | #102180 |
"ast" <nomail@invalid.com> a écrit dans le message de news:56aa1474$0$27833$426a74cc@news.free.fr... OK, thank you for answer I didn't studied metaclass yet that's why it is not clear for me. I have to take a look at that concept first
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-01-29 01:56 +1100 |
| Message-ID | <mailman.57.1453993003.2338.python-list@python.org> |
| In reply to | #102180 |
On Fri, Jan 29, 2016 at 12:56 AM, jmp <jeanmichel@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
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2016-01-28 11:04 -0500 |
| Message-ID | <mailman.61.1453997090.2338.python-list@python.org> |
| In reply to | #102180 |
On Thu, Jan 28, 2016 at 9:56 AM, Chris Angelico <rosuav@gmail.com> wrote: > On Fri, Jan 29, 2016 at 12:56 AM, jmp <jeanmichel@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 > -- > https://mail.python.org/mailman/listinfo/python-list > There is an excellent django book: http://prodjango.com/ There is a section that explains how models are created. Its a great and clear read. -- Joel Goldstick http://joelgoldstick.com/stats/birthdays
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web