Django,通用关系,制作装置

发布于 2024-09-25 01:53:05 字数 440 浏览 0 评论 0原文

我正在尝试为 django-test-utils makefixture 命令添加通用关系和一对一关系支持,这里是源 http://github.com/ericholscher/django-test-utils/blob/master/test_utils/management/commands/makefixture.py< /a>

有人知道如何做到这一点吗?或者可能还有另一种工具可以实现这样的目的:

./manage.py dumpcmd User[:10] > fixtures.json

I'm trying to add generic relations and one-to-one relations support for django-test-utils makefixture command, here is the source http://github.com/ericholscher/django-test-utils/blob/master/test_utils/management/commands/makefixture.py

Does somebody have ideas how to do this? Or may be there is another tool for such thing as:

./manage.py dumpcmd User[:10] > fixtures.json

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偷得浮生 2024-10-02 01:53:05

您有多种解决问题的选择。我将专注于poke-the-code方法,因为我已经有一段时间没有研究过 django 内部结构了。

我已从链接中添加了以下相关代码。请注意,我已经删除了不相关的部分。另请注意,您将要编辑的部分您的案例需要重构。

遵循以下算法,直到您满意为止。

  1. 根据字段将 if 语句重构为(一个或多个)单独的函数。
  2. 添加检查代码,直到找出哪些字段对应于通用关系。
  3. 添加提取代码,直到遵循通用关系。
  4. 测试。

    def handle_models(self, models, **options):
    # SNIP 句柄选项
    
    所有 = 对象
    如果传播:
        收集 = set([(x.__class__, x.pk) for x in all])
        而对象:
            相关 = []
            对于对象中的 x:
                如果调试:
                    print "添加 %s[%s]" % (model_name(x), x.pk)
                # 跟随前向关系字段
                对于 x.__class__._meta.fields + x.__class__._meta.many_to_many 中的 f:
                    # 你的情况在这里
                    if isinstance(f, 外键):
                        new = getattr(x, f.name) # 实例化对象
                        如果是 new 且不在收集中 (new.__class__, new.pk):
                            collected.add((new.__class__, new.pk))
                            相关.追加(新)
                    if isinstance(f, ManyToManyField):
                        对于 getattr(x, f.name).all() 中的新内容:
                            如果是 new 且不在收集中 (new.__class__, new.pk):
                                collected.add((new.__class__, new.pk))
                                相关.追加(新)
                # 截图
            对象=相关
            all.extend(对象)
    
    # SNIP 序列化
    

You have several options how to approach the problem. I'll concentrate on the poke-the-code aproach, since it's been a while since I mucked around with django internals.

I have included the relevant code below from the link. Note that I have removed irrelevant parts. Also note that the part you'll be editing YOUR CASE HERE is in need of a refactor.

Follow the following algorithm until you're satisfied.

  1. Refactor the if statements depending on the fields into (one or more) separate function(s).
  2. Add inspection code until you figure out what fields correspond to generic relations.
  3. Add extraction code until the generic relations are followed.
  4. Test.

    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
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文