Django-model之feild进阶

2018-10-23  本文已影响12人  Eric_Zeng

参考地址

在model中添加字段的格式一般为: field_name = field_type(**field_options)

代码示例

from django.db import models
from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel

class HomePage(Page):
    body = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('body', classname="full"),
    ]

这次主要对body = RichTextField(blank=True) 进行分析,body为对象HomePage的属性,同时body也是对象,在上述代码中定义为RichTextField,且option设置为blank=True

关于feild的options和types

feild为样式对象,options为选项,RichTextField就是field的其中一种(wagtile特有的)

options

1. Field.null

2. Field.blank

3. Field.choices

YEAR_IN_SCHOOL_CHOICES = (
    ('FR', 'Freshman'),
    ('SO', 'Sophomore'),
    ('JR', 'Junior'),
    ('SR', 'Senior'),
)

当然,也可以是多层级嵌套的

4.Field.db_column

5.Field.db_index

6.Field.db_tablespace

7.Field.default

8.Field.editable

9.Field.error_messages

10.Field.help_text

11.Field.primary_key

12.Field.unique

13.Field.unique_for_date

14.Field.verbose

15.Field.validators

Field types

即对象的一些样式

1.AutoField()

2.BooleanField()

3.CharField()

4.DateField()

5.DateTimeField()

6.DecimalField()

7.EmailField(max_length=254, **options)

8.FileField(upload_to=None, max_length=100, **options)

9.FilePathField(path=None, match=None, recursive=False, max_length=100, **options)

10.FloatField()

11.ImageField(upload_to=None, height_field=None, width_field=None, max_length=100, **options)

12.IntegerField()

13.GenericIPAddressField(protocol='both', unpack_ipv4=False, **options)

14.NullBooleanField ()

15.TextField()

16.TimeField(auto_now=False, auto_now_add=False, **options)

16.URLField(max_length=200, **options)

17.UUIDField(**options)

上一篇下一篇

猜你喜欢

热点阅读