1

嗨,我有一个 post save 信号,它在创建新的 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对象正在尝试重新保存该对象?

我怎样才能避免这种冲突?

非常感谢任何帮助。

4

1 回答 1

0

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

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

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

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()

希望这可以帮助任何有类似问题的人。

于 2012-11-07T17:24:27.000 回答