user99874
user99874

Reputation:

Set Django model field with a method

I'm trying to set title_for_url but it shows up in my database as "<property object at 0x027427E0>". What am I doing wrong?

from django.db import models

class Entry(models.Model):

    def _get_title_for_url(self):
        title = "%s" % self.get_title_in_url_format()
        return title

    AUTHOR_CHOICES = (('001', 'John Doe'),)
    post_date = models.DateField()
    author = models.CharField(max_length=3, choices=AUTHOR_CHOICES)
    title = models.CharField(max_length=100, unique=True)
    body = models.TextField()
    image = models.ImageField(upload_to='image/blog')
    image.blank = 'true'
    title_for_url = models.CharField(max_length=100, editable=False, default=property(_get_title_for_url))

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return "/blog/%s/" % self.get_title_in_url_format()        

    def get_title_in_url_format(self):
        "Returns the title as it will be displayed as a URL, stripped of special characters with spaces replaced by '-'."
        import re
        pattern = re.compile( '(\'|\(|\)|,)' )
        titleForUrl = pattern.sub('', self.title)
        pattern = re.compile( '( )' )
        titleForUrl = pattern.sub('-', titleForUrl)
        return titleForUrl.lower()

Upvotes: 0

Views: 5258

Answers (3)

Djangonaut
Djangonaut

Reputation: 5821

title_for_url = models.CharField(max_length=100, editable=False, default=property(_get_title_for_url)

You couldn't do that way.. 'default' should be a value or a calable(with no args)... (property is not calable)

In your case you need to update a save method:

class Entry(models.Model):
    def save(self, *args, **kwargs):
       self.title_for_url = self.get_title_in_url_format()
       super(Entry, self).save(*args, **kwargs)

Upvotes: 3

user99874
user99874

Reputation:

This is the final version of what worked for me:

def save(self, *args, **kwargs):
    self.title = self.title.strip()
    self.title_for_url = self.get_title_in_url_format()
    super(Entry, self).save(*args, **kwargs)

Upvotes: 0

Ned Batchelder
Ned Batchelder

Reputation: 375484

You can't use property() in a default:

.., default=property(_get_title_for_url))

The default value should be a constant. If you need to compute a missing value, use a pre_save hook.

Upvotes: 0

Related Questions