1.models 的字段设置
https://docs.djangoproject.com/en/1.10/ref/models/fields/#field-types
Field types
AutoField:整数自动增长字段,一般用来设置主键(自增长)
An IntegerField that automatically increments according to available IDs.
You usually won’t need to use this directly; a primary key field will automatically
be added to your model if you don’t specify otherwise. See Automatic primary key fields.
BigAutoField:与AutoField 一样,数值更大(1.10中新增的)
New in Django 1.10.
A 64-bit integer, much like an AutoField except
that it is guaranteed to fit numbers from 1 to 9223372036854775807.
BigIntegerField : 与IntegerField 相似,数值更大
A 64-bit integer, much like an IntegerField except
that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807.
The default form widget for this field is a TextInput.
BinaryField :二进制字段,只能保存字节,不能用过滤查询这个字段的值,ModelForm表单中的字段也不能有BinaryField
这个字段也不能替代适当的静态文件处理
A field to store raw binary data. It only supports bytes assignment.
Be aware that this field has limited functionality.
For example, it is not possible to filter a queryset on a BinaryField value.
It is also not possible to include a BinaryField in a ModelForm.
Although you might think about storing files in the database,
consider that it is bad design in 99% of the cases.
This field is not a replacement for proper static files handling.
BooleanField : 布尔值字段 A true/false field.
The default form widget for this field is a CheckboxInput.
If you need to accept null values then use NullBooleanField instead.
The default value of BooleanField is None when Field.default isn’t defined.
CharField :字符字段,必须参数 max_length ,如果是文本,使用TextField
A string field, for small- to large-sized strings.
For large amounts of text, use TextField.
The default form widget for this field is a TextInput.
CharField has one extra required argument:
CharField.max_length
The maximum length (in characters) of the field.
The max_length is enforced at the database level and in Django’s validation.
CommaSeparatedIntegerField : 1.9开始弃用(Deprecated since version 1.9)
DateField : 日期 ,设置auto_now=true 在model.save()会自动更新时间为当前的时间
A date, represented in Python by a datetime.date instance. Has a few extra, optional arguments:
DateField.auto_now
Automatically set the field to now every time the object is saved.
Useful for “last-modified” timestamps. Note that the current date is always used;
it’s not just a default value that you can override.
The field is only automatically updated when calling Model.save().
The field isn’t updated when making updates to other fields in other ways such as QuerySet.update(),
though you can specify a custom value for the field in an update like that.
DateField.auto_now_add
Automatically set the field to now when the object is first created.
Useful for creation of timestamps. Note that the current date is always used;
it’s not just a default value that you can override.
So even if you set a value for this field when creating the object,
it will be ignored. If you want to be able to modify this field,
set the following instead of auto_now_add=True:
DateTimeField :日期时间
A date and time, represented in Python by a datetime.datetime instance.
Takes the same extra arguments as DateField.
The default form widget for this field is a single TextInput.
The admin uses two separate TextInput widgets with JavaScript shortcuts.
DecimalField :大数,两个必须参数 (max_digits=None, decimal_places=None)
DecimalField.max_digits
The maximum number of digits allowed in the number.
Note that this number must be greater than or equal to decimal_places
DecimalField.decimal_places
The number of decimal places to store with the number.
For example, to store numbers up to 999 with a resolution of 2 decimal places, you’d use:
models.DecimalField(..., max_digits=5, decimal_places=2)
And to store numbers up to approximately one billion with a resolution of 10 decimal places:
models.DecimalField(..., max_digits=19, decimal_places=10)
EmailField : 邮箱字段
A CharField that checks that the value is a valid email address.
It uses EmailValidator to validate the input.
FileField :文件字段 (上传文件) A file-upload field.
FileField.upload_to
This attribute provides a way of setting the upload directory and file name,
and can be set in two ways. In both cases, the value is passed to the Storage.save() method.
If you specify a string value, it may contain strftime() formatting,
which will be replaced by the date/time of the file upload (so that uploaded files don’t fill up the given directory).
For example:
class MyModel(models.Model):
# file will be uploaded to MEDIA_ROOT/uploads
upload = models.FileField(upload_to='uploads/')
# or...
# file will be saved to MEDIA_ROOT/uploads/2015/01/30
upload = models.FileField(upload_to='uploads/%Y/%m/%d/')
FileField.storage:
A storage object, which handles the storage and retrieval of your files. See Managing files for details on how to provide this object.
The default form widget for this field is a ClearableFileInput.
TextField:文本字段 A large text field. The default form widget for this field is a Textarea.
TimeField:时间戳字段,A time, represented in Python by a datetime.time instance. Accepts the same auto-population options as DateField.
2.关联字段Relationship fields
ForeignKey:A many-to-one relationship. Requires a positional argument: the class to which the model is related.
ManyToManyField:A many-to-many relationship.
Requires a positional argument: the class to which the model is related,
which works exactly the same as it does for ForeignKey,
including recursive and lazy relationships.
OneToOneField:A one-to-one relationship. Conceptually, this is similar to a ForeignKey with unique=True,
but the “reverse” side of the relation will directly return a single object.
3.静态资源配置 (Configuring static files)
1)Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.
2)In your settings file, define STATIC_URL, for example:
settings.py
STATIC_URL = '/static/'
3)In your templates, either hardcode the url like /static/my_app/example.jpg or, preferably,
use the static template tag to build the URL for the given relative
path by using the configured STATICFILES_STORAGE storage
(this makes it much easier when you want to switch to a content delivery network (CDN)
for serving static files).
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image"/>
4)Store your static files in a folder called static in your app.
For example my_app/static/my_app/example.jpg.
except:要包含自己的静态资源,这些静态资源不放置在app中,那边,需要设置STATICFILES_DIRS变量,并把路径设置在其中
Your project will probably also have static assets that aren’t tied to a particular app.
In addition to using a static/ directory inside your apps,
you can define a list of directories (STATICFILES_DIRS) in your settings file
where Django will also look for static files.
For example:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/var/www/static/',
]
4.使用外部文件引入,导入数据配置文件
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/path/to/my.cnf',
},
}
}
# my.cnf
[client]
database = NAME
user = USER
password = PASSWORD
default-character-set = utf8