更新
这可能更能代表我的问题:
使用loadtestdata,如何填充auth.User数据库?我只想用伪造的用户填充数据库,以及与那些虚假用户相关联的模拟。
我查看了所有相关资源,但我无法取得任何进展。
情况:
我正在使用Django构建仿真模型,我希望存储仿真数据以及参数数据集。应该将许多模拟数据集链接到每个用户,并且可以将许多参数数据集链接到每个模拟。因此,我试图在&model; model.py'我的Django应用程序。
from django.db import models
from django.conf import settings
# Create your models here.
class Simulation(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, )
date = models.DateTimeField()
# Each simulation has only one graph
# Graphing parameters
hill_num = models.CharField(max_length=3)
div_type = models.CharField(max_length=10)
s3_url = models.CharField(max_length=256)
def __str__(self):
return str(self.sim_id)
class Parameter(models.Model):
# Each simulation can have many sets of simulation parameters
simulation = models.ForeignKey('Simulation', on_delete=models.CASCADE)
lsp = models.PositiveIntegerField()
plots = models.PositiveIntegerField()
pioneer = models.BooleanField()
neutral = models.BooleanField()
# for pioneers
p_max = models.PositiveIntegerField()
p_num = models.PositiveIntegerField()
p_start = models.PositiveIntegerField()
# for non-pioneers
np_max = models.PositiveIntegerField()
np_num = models.PositiveIntegerField()
np_start = models.PositiveIntegerField()
def __str__(self):
return str(self.param_id)
./ manage.py makemigrations有效但当我尝试使用python manage.py loadtestdata auth.User:10 divexplorer.Simulation:40 divexplorer.Parameter:300
填充数据库时,会抛出此错误:
auth.User(pk=72): JshtkqSzw3
auth.User(pk=73): QwPfxJc_KS1k5sgH5BN98J
auth.User(pk=74): fuEhnZ
auth.User(pk=75): a
auth.User(pk=76): XjVXXLYGz3MJSfmZ54wGxXo
auth.User(pk=77): fhOWIp
auth.User(pk=78): 5tkEhKOjX2UUbFe
auth.User(pk=79): JgG4Y4PqkcapNJJOlFW1LOQ
auth.User(pk=80): fhRmfQHNim4zM8hGPzpYdkwaHI7
auth.User(pk=81): cEPQtyByKdUs8Gw58DrfNtpsCRB_
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/utils/decorators.py", line 185, in inner
return func(*args, **kwargs)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/autofixture/management/commands/loadtestdata.py", line 225, in handle
autofixture.create(model, count, **kwargs)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/autofixture/__init__.py", line 136, in create
return autofixture.create(count, **create_kwargs)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/autofixture/base.py", line 554, in create
instance = self.create_one(commit=commit, **kwargs)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/autofixture/base.py", line 519, in create_one
instance.save()
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/models/base.py", line 837, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/models/base.py", line 923, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/models/base.py", line 962, in _do_insert
using=using, raw=raw)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
cursor.execute(sql, params)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "user_id" of relation "divexplorer_simulation" does not exist
LINE 1: INSERT INTO "divexplorer_simulation" ("user_id", "date", "hi...
我花了好几个小时试图解决这个错误,但无济于事。有什么想法吗?
我尝试在ForeignKey函数中重命名参数db_column,应用默认值,但它们都不起作用。非常感谢一些投入,非常感谢你!
答案 0 :(得分:0)
我认为您需要指定是否还应使用随机值创建相关模型。
请查看文档:{{3}}
说明的地方:
有一些命令行选项可用。主要是控制相关领域的行为。如果foreingkey或多个到多个字段应填充现有数据,或者相关模型也是动态生成的。有关更多信息,请查看命令的帮助页面:
django-admin.py help loadtestdata
不幸的是,我无法检查正在运行的Django实例,以便为您指出确切的选项及其值,但检查文档我会说您必须使用loadtestdata中的此选项:
...
make_option('--generate-fk', action='store', dest='generate_fk', default=None,
help=u'Do not use already existing instances for ForeignKey relations. ' +
'Create new instances instead. You can specify a comma sperated list of ' +
'field names or ALL to indicate that all foreignkeys should be generated ' +
'automatically.'),
...