迁移错误“操作错误位于/admin/accounts/userstripe/没有这样的表:accounts\u userstripe”

2024-05-15 15:00:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为一家在线商店开发一个网站,在我的帐户模型中,我有两个模型。虽然成功创建了一个模型的表,但没有创建我的UserStripe模型的表,表上显示:

OperationalError at /admin/accounts/userstripe/
no such table: accounts_userstripe

我该怎么办

My models.py:

from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
import stripe
from localflavor.us.us_states import US_STATES

stripe.api_key = settings.STRIPE_SECRET_KEY


class UserStripe(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete= models.CASCADE)
    stripe_id = models.CharField(max_length=120)

    def __unicode__(self):
        return str(self.stripe_id)


class UserAddress(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete= models.CASCADE)
    address = models.CharField(max_length=120)
    address2 = models.CharField(max_length=120)
    city = models.CharField(max_length=120)
    state = models.CharField(max_length=120, choices=US_STATES)
    country = models.CharField(max_length=120)
    zipcode = models.CharField(max_length=25)
    phone = models.CharField(max_length=120)
    shipping = models.BooleanField(default=True)
    billing = models.BooleanField(default=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

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

My signals.py:

import stripe
from django.conf import settings
from django.contrib.auth.signals import user_logged_in
from .models import UserStripe

stripe.api_key = settings.STRIPE_SECRET_KEY


def get_or_create_stripe(sender, user, *args, **kwargs):
    try:
        user.userstripe.stripe_id
    except UserStripe.DoesNotExist:
        customer = stripe.Customer.create(
            email = str(user.email),
        )
        new_user_stripe = UserStripe.objects.create(
            user = user,
            stripe_id = customer.id,
            )
    except:
        pass


user_logged_in.connect(get_or_create_stripe)

My apps.py:

from django.apps import AppConfig


class AccountsConfig(AppConfig):
    name = 'accounts'

    def ready(signals):
        import accounts.signals

有什么我遗漏的吗


Tags: djangofrom模型importidsettingsmodelsdef