0

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

/accounts/signup/ 处的 IntegrityError(1048,“列 'user_id' 不能为空”)

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

谢谢

接收器.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()

模型.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)

用户信号.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",])
4

1 回答 1

1

您使用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,
        )

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

于 2013-04-02T15:00:12.327 回答