class SampleModel(models.Model):
some_data = models.TextField()
如果我使用上面的类(使用自动id
字段),我会得到预期的结果。
>>> from django_dynamic_fixture import G, get
>>> s = G(SampleModel, id=1234, some_data='abcd')
>>> s.id
1234
>>> s.some_data
'abcd'
使用问题中给出的示例模型,得到与问题相同的结果。
指定id
而不是sample_model_id
引发异常。
>>> s = G(SampleModel, id=1234, some_data='abcd')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/falsetru/.virtualenvs/django15/local/lib/python2.7/site-packages/django_dynamic_fixture/__init__.py", line 107, in get
return d.get(model, shelve=shelve, **kwargs)
File "/home/falsetru/.virtualenvs/django15/local/lib/python2.7/site-packages/django_dynamic_fixture/ddf.py", line 507, in get
instance = self.new(model_class, shelve=shelve, named_shelve=named_shelve, **kwargs)
File "/home/falsetru/.virtualenvs/django15/local/lib/python2.7/site-packages/django_dynamic_fixture/ddf.py", line 436, in new
self.set_data_for_a_field(model_class, instance, field, persist_dependencies=persist_dependencies, **configuration)
File "/home/falsetru/.virtualenvs/django15/local/lib/python2.7/site-packages/django_dynamic_fixture/ddf.py", line 348, in set_data_for_a_field
data = self._process_field_with_default_fixture(field, model_class, persist_dependencies)
File "/home/falsetru/.virtualenvs/django15/local/lib/python2.7/site-packages/django_dynamic_fixture/ddf.py", line 335, in _process_field_with_default_fixture
data = self.data_fixture.generate_data(field)
File "/usr/lib/python2.7/code.py", line 216, in interact
sys.ps2
UnsupportedFieldError: polls.models.SampleModel.sample_model_id
更新
解决方法
同时指定id
和sample_model_id
。
>>> s = G(SampleModel, id=1234, sample_model_id=1234, some_data='abcd')
>>> s.sample_model_id
1234
>>> s.some_data
'abcd'
实际上,id
值并没有在内部使用;您可以为 指定任何值id
。
>>> s = G(SampleModel, id=None, sample_model_id=5555, some_data='abcd')
>>> s.sample_model_id
5555
>>> s.some_data
'abcd'