I have following form based in model.
我有基于模型的以下表格。
class Article(models.Model):
title = models.CharField(max_length=100, db_column='title')
url = models.CharField(max_length=100, db_column='url')
category = models.ForeignKey(Category, db_column='category')
description = models.TextField(db_column='description')
createDate = models.DateTimeField(db_column='createDate')
def __unicode__(self):
return self.title
class Meta:
db_table = 'articles'
ordering = ['createDate']
Form
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = ('title', 'description', 'category')
What I want is to validate form and change value of url by lowercasing cleaned title. How can I accomplish that? I appears that url is excluded value, so how can I change it in form after validation?
我想要的是通过降低清理标题来验证表单和更改URL的值。我怎么能做到这一点?我看来url是排除值,所以如何在验证后在表单中更改它?
Thanks.
1 个解决方案
#1
1
You can supply a custom URL when saving an article like this:
保存这样的文章时,您可以提供自定义URL:
class Article(models.Model):
...
def save(self, *args, **kwargs):
if not self.url:
self.url = self.title.lower()
super(Article, self).save(*args, **kwargs)
#1
1
You can supply a custom URL when saving an article like this:
保存这样的文章时,您可以提供自定义URL:
class Article(models.Model):
...
def save(self, *args, **kwargs):
if not self.url:
self.url = self.title.lower()
super(Article, self).save(*args, **kwargs)