Python's Archiver

為方便港臺同胞閱覽,Python中國特別推出簡繁體內容轉換功能

zjxplq 发表于 2008-11-13 08:52

将apps从django 0.96迁移到1.0(一)

[b][size=5]公共改变(common changes)[/size][/b]
[b][size=3]使用unicode[/size][/b]
将字符串('foo')改为unicode字符串(u'foo')。django使用

unicode字符串。虽然raw string大部分时候一样可以工作,

但改为unicode字符串可以避免一些问题

[b][size=3]Models[/size][/b]

将maxlength重命名为max_length
用__unicode__替换__str__
删除了prepopulated_from
  在model字段中删除prepopulated_from参数。它不再有效,已被移到admin.py ModelAdmin类中
删除了core
  model字段中删除core参数。它不再需要。因为它的等价功能改为由admin interface处理。
用admin.py代替class Admin:
  在你的models中删除class Admin内部声明。将在admin中注册apps,将这些声明移到了admin.py中。

例子
0.96下的models.py:[code] class Author(models.Model):
first_name = models.CharField(maxlength=30)
last_name = models.CharField(maxlength=30)
slug = models.CharField(maxlength=60, prepopulate_from=('first_name', 'last_name'))

class Admin:
list_display = ['first_name', 'last_name']

def __str__(self):
return '%s %s' % (self.first_name, self.last_name)
[/code]1.0下的models.py:[code]
  class Author(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    slug = models.CharField(max_length=60)

    def __unicode__(self):
      return u'%s %s' % (self.first_name, self.last_name)
[/code]1.0下的admin.py:[code]
  from django.contrib import admin
  from models import Author

  class AuthorAdmin(admin.ModelAdmin):
    list_display = ['first_name', 'last_name']
    prepopulated_fields = {'slug':('first_name','last_name')}
  admin.site.register(Author,AuthorAdmin)
[/code]

[[i] 本帖最后由 zjxplq 于 2008-11-14 10:50 编辑 [/i]]

xieaotian 发表于 2008-11-13 09:08

好文章,谢谢分享……

zjxplq 发表于 2008-11-13 10:52

有时间慢慢去翻译

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.