我正在尝试为 django-test-utils makefixture 命令添加通用关系和一对一关系支持,这里是源http://github.com/ericholscher/django-test-utils/blob/master/test_utils /管理/命令/makefixture.py
有人知道如何做到这一点吗?或者可能有另一种工具,例如:
./manage.py dumpcmd User[:10] > fixtures.json
我正在尝试为 django-test-utils makefixture 命令添加通用关系和一对一关系支持,这里是源http://github.com/ericholscher/django-test-utils/blob/master/test_utils /管理/命令/makefixture.py
有人知道如何做到这一点吗?或者可能有另一种工具,例如:
./manage.py dumpcmd User[:10] > fixtures.json
你有几个选择如何解决这个问题。我将专注于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