django post save方法导致身份验证错误

时间:2012-11-06 21:37:35

标签: django-models

您好我有一个保存信号,在创建新的User对象时保存user_profile对象:

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    ...

    def __unicode__(self):
        return self.user.username


def _create_user_profile(sender, instance, created, **kwargs):
    UserProfile.objects.create(user=instance)

post_save.connect(_create_user_profile, sender=User)

然而,这导致了以下问题:

如果我在管理员中创建新用户,那一切都很好。如果我然后尝试编辑然后将User权限更改为员工状态,则会出现"Duplicate entry '6' for key 'user_id'"错误。我想UserProfile对象试图重新保存对象?

如何避免这种冲突?

任何帮助都非常感激。

1 个答案:

答案 0 :(得分:0)

好的,所以这有很多帮助:

https://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriver

基本上我需要直接使用<select>找到我想要的<option>xpath。之后我可以模拟click事件:

self.browser.find_element_by_xpath(
     "//select[@id='my_select_id']/option[text()='my_option_text']"
).click()

如果文本字符串未知,我也可以通过选项索引进行选择:

self.browser.find_element_by_xpath(
    "//select[@id='id_module']/option[2]"
).click()

希望这有助于任何有类似问题的人。