开发者

initial data in model field

开发者 https://www.devze.com 2023-01-24 21:02 出处:网络
I have a simple model: class MediaLink(models.Model): title = models.CharField(max_length=200) subtitle = models.TextField(max_length=2000, unique=False, blank=True)

I have a simple model:

class MediaLink(models.Model): 
  title = models.CharField(max_length=200)
  subtitle = models.TextField(max_length=2000, unique=False, blank=True)
  byline = models.CharField(max_length=200, unique=False, blank=True)
  url = models.URLField(unique=False) 
  source = models.CharField(max_length=30, unique=False)
  source_url = models.CharField(max_length=30, unique=False, blank=True, null=True, choices=SURL_CHOICES)
  mediatype = models.CharField(max_length=30, choices=MEDIATYPE_CHOICES)
  topic = models.CharField(max_length=30, choices=TOPIC_CHOICES)
  sourceinfo = models.ForeignKey(SourceInfo, blank=True, null=True)
  date_added = models.DateField(auto_now_add=True)


  def __unicode__(self):
    return u'%s' % self.title

  class Meta: 
    abstract = True

  class Admin: 
    pass

I'd like to set the "subtitle" field so that in eac开发者_如何转开发h object, its initial data is "<h3></h3>". I'm using markdown and having the tags set initially saves me from having to create them in the admin for each record.


You could just set the default on the field:

subtitle = models.TextField(max_length=2000, unique=False, blank=True, default='<h3></h3>')

Or if you're using a ModelForm you can set it in the initial kwarg.

0

精彩评论

暂无评论...
验证码 换一张
取 消