D
D
Dauren S2016-07-30 15:04:38
Django
Dauren S, 2016-07-30 15:04:38

Django translit categories?

class Category(models.Model):
    title = models.CharField(max_length=200)
    url = models.CharField(max_length=200,blank=True, null=True)
    parent = models.ForeignKey('Category',blank=True, null=True)
    description=models.CharField(max_length=200,blank=True, null=True)
    path=models.CharField(max_length=200,blank=True, null=True)
    metadesc=models.CharField(max_length=200,default='',blank=True, null=True)
    published=models.BooleanField(default=True)
    position=models.IntegerField(default=0)

    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        url=slugify(self.title, 'ru')
        self.url = url
        super(Category, self).save(*args, **kwargs)

In the save method, the name is transliterated and the category url is written as a transliteration of the name. Question: How can I make this method work only if the url is empty.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dauren S, 2016-07-30
@dauren101

Happened. Here is my option.

def save(self, *args, **kwargs):
        if not self.url:
            url=slugify(self.title, 'ru')
            self.url = url
            super(Category, self).save(*args, **kwargs)
        else:
            self.url = self.url
            super(Category, self).save(*args, **kwargs)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question