在我将class BillingAddress
的名称更改为class Address
之后。并尝试了这些阶段
从应用程序的迁移目录中删除所有迁移记录。
还删除了db.sqlite3文件。
然后,我做了以下命令
python manage.py makemigrations core
python manage.py migrate
迁移后,不显示插入的数据,而是显示
No migrations to apply.
在终端机
PS C:\Users\Dell\project5> python manage.py makemigrations core
Migrations for 'core':
core\migrations\0001_initial.py
- Create model Address
- Create model Coupon
- Create model Item
- Create model Order
- Create model Refund
- Create model Payment
- Create model OrderItem
- Add field items to order
- Add field payment to order
- Add field shipping_address to order
- Add field user to order
PS C:\Users\Dell\project5> python manage.py migrate
Operations to perform:
Apply all migrations: account, admin, auth, contenttypes, core, sessions, sites, socialaccount
Running migrations:
No migrations to apply.
PS C:\Users\Dell\project5>
当我单击add to cart
按钮时,它显示为错误
在/ add-to-cart / test-product-4 /下的OperationalError 表core_order没有名为shipping_address_id的列
model.py
from django.conf import settings
from django.db import models
from django.db.models import Sum
from django.shortcuts import reverse
from django_countries.fields import CountryField
# Create your models here.
CATEGORY_CHOICES = (
('SB', 'Shirts And Blouses'),
('TS', 'T-Shirts'),
('SK', 'Skirts'),
('HS', 'Hoodies&Sweatshirts')
)
LABEL_CHOICES = (
('S', 'sale'),
('N', 'new'),
('P', 'promotion')
)
ADDRESS_CHOICES = (
('B', 'Billing'),
('S', 'Shipping')
)
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.FloatField()
discount_price = models.FloatField(blank=True, null=True)
category = models.CharField(choices=CATEGORY_CHOICES, max_length=2)
label = models.CharField(choices=LABEL_CHOICES, max_length=1)
slug = models.SlugField()
description = models.TextField()
image = models.ImageField()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("core:product", kwargs={
'slug': self.slug
})
def get_add_to_cart_url(self):
return reverse("core:add-to-cart", kwargs={
'slug': self.slug
})
def get_remove_from_cart_url(self):
return reverse("core:remove-from-cart", kwargs={
'slug': self.slug
})
class OrderItem(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
ordered = models.BooleanField(default=False)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
def __str__(self):
return f"{self.quantity} of {self.item.title}"
def get_total_item_price(self):
return self.quantity * self.item.price
def get_total_discount_item_price(self):
return self.quantity * self.item.discount_price
def get_amount_saved(self):
return self.get_total_item_price() - self.get_total_discount_item_price()
def get_final_price(self):
if self.item.discount_price:
return self.get_total_discount_item_price()
return self.get_total_item_price()
class Order(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
ref_code = models.CharField(max_length=20)
items = models.ManyToManyField(OrderItem)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
shipping_address = models.ForeignKey(
'Address', related_name='shipping_address', on_delete=models.SET_NULL, blank=True, null=True)
billing_address = models.ForeignKey(
'Address', related_name='billing_address', on_delete=models.SET_NULL, blank=True, null=True)
payment = models.ForeignKey(
'Payment', on_delete=models.SET_NULL, blank=True, null=True)
coupon = models.ForeignKey(
'Coupon', on_delete=models.SET_NULL, blank=True, null=True)
being_delivered = models.BooleanField(default=False)
received = models.BooleanField(default=False)
refund_requested = models.BooleanField(default=False)
refund_granted = models.BooleanField(default=False)
'''
1. Item added to cart
2. Adding a BillingAddress
(Failed Checkout)
3. Payment
4. Being delivered
5. Received
6. Refunds
'''
def __str__(self):
return self.user.username
def get_total(self):
total = 0
for order_item in self.items.all():
total += order_item.get_final_price()
if self.coupon:
total -= self.coupon.amount
return total
class Address(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
street_address = models.CharField(max_length=100)
apartment_address = models.CharField(max_length=100)
country = CountryField(multiple=False)
zip = models.CharField(max_length=100)
address_type = models.CharField(max_length=1, choices=ADDRESS_CHOICES)
default = models.BooleanField(default=False)
def __str__(self):
return self.user.username
class Meta:
verbose_name_plural = 'Address'
class Payment(models.Model):
stripe_charge_id = models.CharField(max_length=50)
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL, blank=True, null=True)
amount = models.FloatField()
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.user.username
class Coupon(models.Model):
code = models.CharField(max_length=15)
amount = models.FloatField()
def __str__(self):
return self.code
class Refund(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
reason = models.TextField()
accepted = models.BooleanField(default=False)
email = models.EmailField()
def __str__(self):
return f"{self.pk}"
settting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'crispy_forms',
'django_countries',
'core'
]
答案 0 :(得分:0)
这不应该一直执行,但是您可以删除迁移文件夹上的所有迁移,但不能删除init.py。之后,您可以再次运行迁移,它应该可以工作。
答案 1 :(得分:0)
您可以执行此操作,删除应用程序核心上的所有迁移文件,而不是初始化文件。 删除数据库sqlite和shell。 python manage.py迁移 python manage.py makemigration和迁移