Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102183

Re: class attribute

From jmp <jeanmichel@sequans.com>
Newsgroups comp.lang.python
Subject Re: class attribute
Date 2016-01-28 14:56 +0100
Message-ID <mailman.54.1453989841.2338.python-list@python.org> (permalink)
References <56aa1474$0$27833$426a74cc@news.free.fr>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web