Django升级到1.0以后的一些变化
最近几天在玩django,从网上找到limodou的django step by step中文教程,由于该教程是基于0.9x的,而我安装的django是1.02,里面有一些改动,目前主要的地方有:
1.maxlength改成了max_length;
2.radio_admin已经没有了,在google上查了,应该改成如下:
原文地址:http://hi.baidu.com/zhoujialiang/blog/item/7bb24b22ead660ad4723e82e.html
旧代码:
#coding=utf-8
from django.db import models
# Create your models here.
class Address(models.Model):
name = models.CharField('姓名', maxlength=6, unique=True)
gender = models.CharField('性别', choices=(('M', '男'), ('F', '女')),
maxlength=1, radio_admin=True)
telphone = models.CharField('电话', maxlength=20)
mobile = models.CharField('手机', maxlength=11)
from django.db import models
# Create your models here.
class Address(models.Model):
name = models.CharField('姓名', maxlength=6, unique=True)
gender = models.CharField('性别', choices=(('M', '男'), ('F', '女')),
maxlength=1, radio_admin=True)
telphone = models.CharField('电话', maxlength=20)
mobile = models.CharField('手机', maxlength=11)
新的代码:
1 #coding=utf-8
2 from django.db import models
3 from django.contrib import admin
4 from django.forms import ModelForm
5 from django import forms
6
7 # Create your models here.
8
9 GENDER_CHOICES = (
10 ('M','男'),
11 ('F','女'),
12 )
13
14
15 class Address(models.Model):
16 name = models.CharField('姓名',max_length=20, unique=True)
17 gender = models.CharField('性别',choices=GENDER_CHOICES,max_length=1)
18 telephone = models.CharField('电话', max_length=20)
19 mobile = models.CharField('手机', max_length=11)
20 room = models.CharField('房间', max_length=10)
21
22
23 class AddressForm(ModelForm):
24 gender = forms.ChoiceField(label='性别',choices=GENDER_CHOICES,widget=forms.RadioSelect(),initial='M')
25
26 class Meta:
27 model = Address
28
29 class AddressAdmin(admin.ModelAdmin):
30 form = AddressForm
31
32 admin.site.register(Address,AddressAdmin)
2 from django.db import models
3 from django.contrib import admin
4 from django.forms import ModelForm
5 from django import forms
6
7 # Create your models here.
8
9 GENDER_CHOICES = (
10 ('M','男'),
11 ('F','女'),
12 )
13
14
15 class Address(models.Model):
16 name = models.CharField('姓名',max_length=20, unique=True)
17 gender = models.CharField('性别',choices=GENDER_CHOICES,max_length=1)
18 telephone = models.CharField('电话', max_length=20)
19 mobile = models.CharField('手机', max_length=11)
20 room = models.CharField('房间', max_length=10)
21
22
23 class AddressForm(ModelForm):
24 gender = forms.ChoiceField(label='性别',choices=GENDER_CHOICES,widget=forms.RadioSelect(),initial='M')
25
26 class Meta:
27 model = Address
28
29 class AddressAdmin(admin.ModelAdmin):
30 form = AddressForm
31
32 admin.site.register(Address,AddressAdmin)