如何防止灯具与django post_save信号码冲突? [英] How do I prevent fixtures from conflicting with django post_save signal code?

查看:117
本文介绍了如何防止灯具与django post_save信号码冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,当新用户注册时,我想在某些表中创建条目。例如,我想创建一个用户配置文件,然后引用他们的公司和一些其他记录。我实现了一个post_save信号:

In my application, I want to create entries in certain tables when a new user signs up. For instance, I want to create a userprofile which will then reference their company and some other records for them. I implemented this with a post_save signal:

def callback_create_profile(sender, **kwargs):
    # check if we are creating a new User
    if kwargs.get('created', True):
        user = kwargs.get('instance')
        company = Company.objects.create(name="My Company")
        employee = Employee.objects.create(company=company, name_first=user.first_name, name_last=user.last_name)
        profile = UserProfile.objects.create(user=user, employee=employee, partner=partner)
# Register the callback
post_save.connect(callback_create_profile, sender=User, dispatch_uid="core.models")

运行时运行良好。我可以使用管理员创建一个新的用户,而其他三个表也有明智的条目。 (除此之外,员工自从user.first_name和user.last_name在管理员的表单中保存时未填写,我仍然不明白为什么这样做)

This works well when run. I can use the admin to create a new user and the other three tables get entries with sensible as well. (Except that is, the employee since the user.first_name and user.last_name aren't filled out in the admin's form when it saves. I still don't understand why it is done like that)

当我运行测试套件时出现问题。在此之前,我创建了一堆灯具来在表格中创建这些条目。现在我收到一条错误,指出:

The problem came when I ran my test suite. Before this, I had created a bunch of fixtures to create these entries in the tables. Now I get an error that states:

IntegrityError: duplicate key value violates unique constraint "core_userprofile_user_id_key"

我认为这是因为我已经在id为1的fixture中创建了一个公司,员工和个人资料记录,现在是post_save信号正在尝试重新创建它。

I think this is because I have already created a company,employee and profile records in the fixture with id "1" and now the post_save signal is trying to recreate it.

我的任务是:在运行灯具时可以禁用此post_save信号吗?我可以检测到我正在运行作为测试套件的一部分,而不是创建这些记录?我现在应该从灯具中删除这些记录(虽然信号只设置默认值不是我想要测试的值)?为什么夹具加载代码只是覆盖创建的记录?

My questios are: can I disable this post_save signal when running fixtures? Can I detect that I am running as part of the test suite and not create these records? Should I delete these records from the fixtures now (although the signal only sets defaults not the values I want to be testing against)? Why doesn't the fixture loading code just overwrite the created records?

人们如何做?

推荐答案

我想我有办法做到这一点。在传送的kwargs中有一个'raw'参数和信号,所以我可以用以下代码替换上面的测试:

I think I figured out a way to do this. There is a 'raw' parameter in the kwargs passed in along with signals so I can replace my test above with this one:

if (kwargs.get('created', True) and not kwargs.get('raw', False)):

在loaddata运行时使用Raw。这似乎是伎俩。

Raw is used when loaddata is running. This seems to do the trick.

这里提到: http://code.djangoproject.com/ticket/13299

如果记录下来,将会很好: http://docs.djangoproject.com/en/1.2/ref/signals/ #django.db.models.signals.post_save

Would be nice if this was documented: http://docs.djangoproject.com/en/1.2/ref/signals/#django.db.models.signals.post_save

这篇关于如何防止灯具与django post_save信号码冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆