我尝试使用 Django-seed
生成假电话号码:
这是创建我的种子的函数
def create_users():
seeder = Seed.seeder()
seeder.add_entity(User, 4, {
'mobile_number': PhoneNumber.from_string("+41 79 123 45 67"),
'email': lambda x: seeder.fake.email(),
'is_superuser': False,
'is_staff': False,
'is_active': True,
})
seeder.execute()
我也尝试过:'mobile_number': "+43 79 123 XX XX"
无论我尝试什么,我都会收到此错误:
Traceback (most recent call last):
File ".../pycharm/django_manage.py", line 59, in <module>
run_command()
File ".../pycharm/django_manage.py", line 46, in run_command
run_module(manage_file, None, '__main__', True)
File ".../anaconda3/lib/python3.6/runpy.py", line 205, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File ".../anaconda3/lib/python3.6/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File ".../anaconda3/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File ".../manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File ".../venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File ".../venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File ".../venv/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File ".../venv/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File ".../event_manager/management/commands/seed.py", line 21, in handle
run_seed(self, options['mode'])
File ".../event_manager/management/commands/seed.py", line 36, in run_seed
create_users()
File ".../event_manager/management/commands/seed.py", line 57, in create_users
'is_active': True,
File ".../venv/lib/python3.6/site-packages/django_seed/seeder.py", line 132, in add_entity
File ".../python3.6/site-packages/django_seed/seeder.py", line 60, in guess_field_formatters
formatter = field_type_guesser.guess_format(field)
File ".../python3.6/site-packages/django_seed/guessers.py", line 107, in guess_format
raise AttributeError(field)
AttributeError: users.User.mobile_number
这是用户模型:
from phonenumber_field.modelfields import PhoneNumberField
class User(AbstractBaseUser, PermissionsMixin):
...
mobile_number = PhoneNumberField(_("Mobile phone number"), blank=True, null=True, help_text=_("International format with country code (starting with \"+\"). Ex. +43 71 123 45 XX."))
谁能指出我缺少什么?
最佳答案
发生错误是因为 mobile_number
不是模型 User
的字段(至少在默认的 django User 模型中不是);错误出现在您的代码的这一部分中:
seeder.add_entity(User, 4, {
'mobile_number': ...,
...
})
您需要指定具有该字段的另一个模型,或者从对 seeder.add_entity()
的调用中更改/删除字段 mobile_number
。
关于python - 在 Django-seed 中生成 mobile_number 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55077344/