大家好!
下面是一个使用造假者填充模型的脚本。 Faker允许使用“ address_class”参数。指定address_class ='a'将其范围缩小,但仅缩小到10.0.0.0/8。如何进一步调整发电机,例如调整到10.10.10.0/24?
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
django.setup()
from workplace.models import IpAddress
import factory
import faker
from faker.providers import internet
faker = faker.Factory.create()
class IpAddressFactory(factory.django.DjangoModelFactory):
class Meta:
model = IpAddress
django_get_or_create = ('ip_address',)
ip_address = faker.ipv4_private(address_class='a')
ip = IpAddressFactory()
print(ip)
答案 0 :(得分:0)
与其说是适当的解决方案,倒不如说是一种解决方法:
ipv4_private
使用_IPv4Constants._private_networks
中的类providers.internet
在_private_networks
中调整class IPv4Constants
可以解决问题
/lib/python3.7/site-packages/faker/providers/internet/ init .py`
class _IPv4Constants:
"""
IPv4 network constants used to group networks into different categories.
Structure derived from `ipaddress._IPv4Constants`.
Excluded network list is updated to comply with current IANA list of
private and reserved networks.
"""
# Three common private networks from class A, B and CIDR
# to generate private addresses from.
_private_networks = [
# ip_network('10.0.0.0/8'),
# ip_network('172.16.0.0/12'),
# ip_network('192.168.0.0/16'),
ip_network('10.10.10.0/24'),
]