Rom*_*giy 7 django fixtures django-testing django-fixtures
我正在尝试为django-test-utils makefixture命令添加泛型关系和一对一关系支持,这里是源代码http://github.com/ericholscher/django-test-utils/blob/master/test_utils /management/commands/makefixture.py
有人有想法如何做到这一点?或者可能有另一种工具用于:
./manage.py dumpcmd User[:10] > fixtures.json
Run Code Online (Sandbox Code Playgroud)
您有多种解决问题的选择。我将专注于poke-the-code方法,因为我已经有一段时间没有研究过 django 内部结构了。
我已从链接中添加了以下相关代码。请注意,我已经删除了不相关的部分。另请注意,您将在此处编辑案例的部分需要重构。
遵循以下算法,直到您满意为止。
if根据字段将语句重构为(一个或多个)单独的函数。测试。
def handle_models(self, models, **options):
# SNIP handle options
all = objects
if propagate:
collected = set([(x.__class__, x.pk) for x in all])
while objects:
related = []
for x in objects:
if DEBUG:
print "Adding %s[%s]" % (model_name(x), x.pk)
# follow forward relation fields
for f in x.__class__._meta.fields + x.__class__._meta.many_to_many:
# YOU CASE HERE
if isinstance(f, ForeignKey):
new = getattr(x, f.name) # instantiate object
if new and not (new.__class__, new.pk) in collected:
collected.add((new.__class__, new.pk))
related.append(new)
if isinstance(f, ManyToManyField):
for new in getattr(x, f.name).all():
if new and not (new.__class__, new.pk) in collected:
collected.add((new.__class__, new.pk))
related.append(new)
# SNIP
objects = related
all.extend(objects)
# SNIP serialization
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
611 次 |
| 最近记录: |