class attribute
jmp
jeanmichel at sequans.com
Thu Jan 28 08:56:37 EST 2016
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
More information about the Python-list
mailing list