GenericIPAddressField – Django 模型
GenericIPAddressField是一个以字符串格式存储IPv4 或 IPv6 地址的字段(例如 192.0.2.30 或 2a02:42fe::4)。此字段的默认表单小部件是 TextInput。 IPv6 地址规范化遵循 RFC 4291#section-2.2 第 2.2 节,包括使用该节第 3 段中建议的 IPv4 格式,例如::ffff:192.0.2.0
。例如, 2001:0::0:01
将被标准化为2001::1
, ::ffff:0a0a:0a0a
被标准化为::ffff:10.10.10.10
。所有字符都转换为小写。
句法:
field_name = models.GenericIPAddressField(**options)
GenericIPAddressField 接受以下参数:
GenericIPAddressField.protocol
将有效输入限制为指定协议。接受的值为“ both ”(默认)、“ IPv4 ”或“ IPv6 ”。匹配不区分大小写。
GenericIPAddressField.unpack_ipv4
解包 IPv4 映射地址,例如::ffff:192.0.2.1 。如果启用此选项,该地址将被解压缩到192.0.2.1 。默认为禁用。只能在协议设置为“两者”时使用。
Django Model GenericIPAddress字段说明
使用示例说明 GenericIPAddressField。考虑一个名为geeks
的项目,它有一个名为geeksforgeeks
的应用程序。
Refer to the following articles to check how to create a project and an app in Django.
- How to Create a Basic Project using MVT in Django?
- How to Create an App in Django ?
在极客应用的models.py
文件中输入以下代码。
from django.db import models
from django.db.models import Model
# Create your models here.
class GeeksModel(Model):
geeks_field = models.GenericIPAddressField()
将极客应用添加到INSTALLED_APPS
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'geeks',
]
现在,当我们从终端运行makemigrations
命令时,
Python manage.py makemigrations
将在geeks
目录中创建一个名为 migrations 的新文件夹,其中包含一个名为0001_initial.py
的文件
# Generated by Django 2.2.5 on 2019-09-25 06:00
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name ='GeeksModel',
fields =[
('id',
models.AutoField(
auto_created = True,
primary_key = True,
serialize = False,
verbose_name ='ID'
)),
('geeks_field',
models.GenericIPAddressField()),
],
),
]
现在运行,
Python manage.py migrate
因此,当您在项目上运行迁移时,会创建一个geeks_field
GenericIPAddressField 。它是一个存储整数的字段。
如何使用 GenericIPAddressField ?
GenericIPAddressField 用于以字符串格式存储 Pv4 或 IPv6 地址,因此它基本上是一个带有 IP 地址验证的 CharField。让我们尝试在此字段中保存一个 IP 地址“ 0.0.0.0 ”。
# importing the model
# from geeks app
from geeks.models import GeeksModel
# creating an instance of
# IP addresses
d = "0.0.0.0"
# creating a instance of
# GeeksModel
geek_object = GeeksModel.objects.create(geeks_field = d)
geek_object.save()
现在让我们在管理服务器中检查它。我们已经创建了 GeeksModel 的一个实例。
字段选项
字段选项是赋予每个字段的参数,用于应用某些约束或将特定特征赋予特定字段。例如,向 GenericIPAddressField 添加参数null = True
将使其能够在关系数据库中存储该表的空值。
以下是 GenericIPAddressField 可以使用的字段选项和属性。
Field Options | Description |
---|---|
Null | If True, Django will store empty values as NULL in the database. Default is False. |
Blank | If True, the field is allowed to be blank. Default is False. |
db_column | The name of the database column to use for this field. If this isn’t given, Django will use the field’s name. |
Default | The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created. |
help_text | Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. |
primary_key | If True, this field is the primary key for the model. |
editable | If False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True. |
error_messages | The error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override. |
help_text | Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. |
verbose_name | A human-readable name for the field. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces. |
validators | A list of validators to run for this field. See the validators documentation for more information. |
Unique | If True, this field must be unique throughout the table. |
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。