Django接收器列'user_id'不能为空

时间:2013-04-02 14:47:58

标签: django django-signals

我正在使用userena接收器,但我收到以下错误...

/ accounts / signup /的

IntegrityError (1048,“列'user_id'不能为空”)

我假设我没有在我自己的receiver.py中正确访问用户。有人可以看看吗?

由于

receivers.py

from django.dispatch import receiver
from userena.signals import signup_complete

from accounts.models import Account

@receiver(signup_complete)
def handle_signup_complete(user, **kwargs):
    try:
        account = Account.objects.get(user=user)
    except Account.DoesNotExist:
        account = Account.objects.create()
        account.credit_limit = 0
        account.balance = 0
        account.user = user
        account.save()

models.py

class Account(models.Model):

    OPEN, FROZEN, CLOSED = 'Open', 'Frozen', 'Closed'
    status = models.CharField(max_length=32, default=OPEN)
    credit_limit = models.DecimalField(max_digits=10, decimal_places=1,
                                       null=True, blank=True)
    # For performance, we keep a cached balance
    balance = models.DecimalField(max_digits=10, decimal_places=1,
                                  null=True, blank=True)
    #FK
    user = models.OneToOneField(User)

userena signals.py

from django.dispatch import Signal

signup_complete = Signal(providing_args=["user",])
activation_complete = Signal(providing_args=["user",])
confirmation_complete = Signal(providing_args=["user","old_email"])
password_complete = Signal(providing_args=["user",])

1 个答案:

答案 0 :(得分:1)

您使用create错误,您应该将值作为参数传递给create。 https://docs.djangoproject.com/en/1.5/ref/models/querysets/#django.db.models.query.QuerySet.create

@receiver(signup_complete)
def handle_signup_complete(user, **kwargs):
    try:
        account = Account.objects.get(user=user)
    except Account.DoesNotExist:
        account = Account.objects.create(
            user=user,
            credit_limit=0,
            balance=0,
        )

基本上,问题是在创建对象时需要传递任何必填字段。