Django models & Python class attributes

Posted by Geo on Stack Overflow See other posts from Stack Overflow or by Geo
Published on 2010-05-25T10:06:44Z Indexed on 2010/05/25 10:11 UTC
Read the original article Hit count: 237

Filed under:
|
|

The tutorial on the django website shows this code for the models:

from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()

Now, each of those attribute, is a class attribute, right? So, the same attribute should be shared by all instances of the class. A bit later, they present this code:

class Poll(models.Model):
    # ...
    def __unicode__(self):
        return self.question

class Choice(models.Model):
    # ...
    def __unicode__(self):
        return self.choice

How did they turn from class attributes into instance attributes? Did I get class attributes wrong?

© Stack Overflow or respective owner

Related posts about python

Related posts about django