Django错误:即使进行了迁移,也没有这样的表

时间:2019-08-15 06:47:03

标签: python django python-3.x django-models django-views

我在访问http://127.0.0.1:8000/orders/create/时遇到操作错误

我得到的错误:

Samples

我已经尝试进行迁移。但是问题不会消失

我的models.py看起来像这样:

OperationalError at /orders/create/
no such table: orders_order

和我的views.py看起来像这样:

from django.db import models
from ecom.models import Product

# Create your models here.

class Order(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.EmailField()
    address = models.CharField(max_length=250)
    postal_code = models.CharField(max_length=20)
    city = models.CharField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    paid = models.BooleanField(default=False)

    class Meta:
        ordering = ('-created',)

    def __str__(self):
        return 'Order {}'.format(self.id)

    def get_total_cost(self):
        return sum(item.get_cost() for item in self.items.all())

#item info 
class OrderItem(models.Model):
    order = models.ForeignKey(Order, related_name='items',on_delete=models.PROTECT)
    product = models.ForeignKey(Product,related_name='order_items',on_delete=models.PROTECT)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    quantity = models.PositiveIntegerField(default=1)

    def __str__(self):
        return '{}'.format(self.id)

    def get_cost(self):
        return self.price * self.quantity

Django突出显示的行:

from django.shortcuts import render
from .models import OrderItem
from .forms import OrderCreateForm
from cart.cart import Cart

# Create your views here.
def order_create(request):
    cart = Cart(request)
    if request.method == 'POST':
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            order = form.save()
            for item in cart:
                OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity'])
            # clear the cart            
            cart.clear()
            return render(request, 'order/created.html', {'order': order})
    else:
        form = OrderCreateForm()
        return render(request, 'order/create.html', {'cart': cart, 'form': form})

1 个答案:

答案 0 :(得分:1)

我通过在python manage.py migrate之后运行python manage.py makemigrations orders来解决此问题。另外,请确保您的应用包含在settings.py文件中。