0

次のコードがあります。

class SampleModel(models.Model):
    sample_model_id = models.AutoField(primary_key=True)
    some_date = models.TextField()

def some_function():
    s = G(SampleModel, sample_model_id=1234, some_data='abcd')
    assert s.sample_model_id == 1
    assert s.some_data == 'abcd'

assert ステートメントはパスします (true ステートメントです)。sample_model_id を設定できない理由を教えてください。

Python 2.7、Django 1.4.5、および django-dynamic-fixture 1.6.5 (最新バージョン) を使用しています。

4

1 に答える 1

2
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'
于 2013-08-02T19:23:20.950 に答える