AJW
AJW

Reputation: 5863

Auto create slug field in django 1.5 models - example from django tutorial

I am quite new to Django and I am trying to auto create a slug field in django models. So, following the django 101 tutorial, I have tried to create the slug field from the following models.py

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    slugp = models.SlugField(max_length=255, unique=True)

    def __unicode__(self):
         return self.question

    def save(self, *args, **kwargs):
        self.slugp = slugify(self.question)
        super(Poll, self).save(*args, **kwargs)

and then I use the django API as follows:

from writedb.models import Poll, Choice 
from django.utils import timezone
p = Poll(question="What's new?", pub_date=timezone.now())
p.save
# out: <bound method Poll.save of <Poll: What's new?>>
p.slugp
#out: u''

Whatever I do the slugp field does not seem to get populated (or is the way I am accesing it wrong? I dont seem the field being populated in the database either) - I am wondering what I am doing wrong. Any pointers would be much appreciated - and sorry if this is a 101 question.

Upvotes: 1

Views: 3657

Answers (1)

Timmy O&#39;Mahony
Timmy O&#39;Mahony

Reputation: 53990

You aren't calling the save method correctly. p.save needs to be p.save(). The former will just return the contents of the save attribute which is the method itself, where as the latter actually executes the method.

Upvotes: 4

Related Questions