简体   繁体   English

Natural Key 导致 Django 测试失败

[英]Natural Key causes Django tests to fail

Consider a clean django 1.7.7 project with one app called testrunner.考虑一个干净的 django 1.7.7 项目,其中包含一个名为 testrunner 的应用程序。

The models look like this:模型如下所示:

class Contact(AbstractBaseUser, PermissionsMixin, models.Model):
    relation = models.ForeignKey('tests.Relation', related_name='contacts')
    username = models.CharField(max_length=200, unique=True)
    first_name = models.CharField(max_length=30, null=True, blank=True)
    last_name = models.CharField(max_length=30, null=True, blank=True)
    email = models.EmailField(null=True, blank=True, unique=True)

    is_staff = models.BooleanField('staff status', default=False,
                                   help_text='Designates whether the user can log into this admin site.')
    is_active = models.BooleanField('active', default=True,
                                    help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.')

    USERNAME_FIELD = 'username'

    def get_full_name(self):
        pass

    def get_short_name(self):
        pass

    def get_name(self):
        pass

class Relation(models.Model):
    name = models.CharField(max_length=200, unique=True)
    created_by = models.ForeignKey('tests.Contact', null=True, related_name='created_%(class)s')
    modified_by = models.ForeignKey('tests.Contact', null=True, related_name='modified_%(class)s')

    def natural_key(self):
        return (self.name,)

In the settings.py I've set 'tests.Contact' to by the AUTH_USER_MODEL .在 settings.py 中,我已将'tests.Contact'设置为AUTH_USER_MODEL

This setup is a clean test to replicate the error I get within a larger environment.此设置是一个干净的测试,用于复制我在更大环境中遇到的错误。 The problem is that I cannot run the django tests without it failing on the creation of the test database:问题是我无法运行 django 测试,除非它在创建测试数据库时失败:

manage.py test
Testing started at 14:39 ...
Creating test database for alias 'default'...
CommandError: Can't resolve dependencies for tests.Contact, admin.LogEntry, tests.Relation in serialized app list.

Process finished with exit code 1
Empty test suite.

When I remove the def natural_key(self) from the Relation model everything works fine.当我从 Relation 模型中删除def natural_key(self) ,一切正常。 We would like to use the natural_key on the Relation model for our fixtures, but are unable to get it to work with django tests.我们想在我们的装置的 Relation 模型上使用 natural_key,但无法让它与 django 测试一起工作。

What am I doing wrong?我究竟做错了什么?

A very simple solution, yet took me some time to come up with.一个非常简单的解决方案,但我花了一些时间才想出。

tl;dr tl;博士

  • Dynamically import serializers .动态导入序列化程序
  • Dynamically define natural_keys method & assign to model.动态定义natural_keys方法并分配给模型。

def method_where_serializers_is_being_used():
    # import serlializers
    from django.core import serializers
    
    # define natural_key method
    def natural_key(self):
        return some_unique_id

    DjangoModel.natural_key = natural_key  

It's a kind of a hack, but its working fine这是一种黑客,但它工作正常

The problem here is a circular dependency involving natural keys that Django fails to handle properly.这里的问题是涉及 Django 无法正确处理的自然键的循环依赖。 Since this post was made, the loading of such data was already much improved, but serialization still breaks on a (now pretty obsolete) check for circular dependencies.自从发布了这篇文章,这些数据的加载已经有了很大的改进,但是序列化仍然在(现在非常过时的)循环依赖检查中中断。

This is an open bug and reported here: https://code.djangoproject.com/ticket/31051这是一个开放的错误并在此处报告: https : //code.djangoproject.com/ticket/31051

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM