models.py
from django.conf import settings
from django.db.models import Sum
from django.db import models
from books.models import Book
class OrderItem(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
def __str__(self):
return self.book.title
class Order(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
is_ordered = models.BooleanField(default=False)
items = models.ManyToManyField(OrderItem)
ref_code = models.CharField(max_length=50)
def __str__(self):
return self.user.username
def get_total(self):
return self.items.all().aggregate(order_total=Sum('book__price'))['order_total']
class Payment(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
amount = models.FloatField()
date_paid = models.DateTimeField(auto_now_add=True)
stripe_charge_id = models.CharField(max_length=100)
def __str__(self):
return self.stripe_charge_id
模板
{% extends 'base.html' %}
{% block breadcrumb %}
<li class="breadcrumb-item" aria-current="page">
<a href="{% url 'books:book-list' %}">Book List</a></li>
<li class="breadcrumb-item active" aria-current="page">{{ book.title }}</li>
{% endblock %}
{% block content %}
<div class='row'>
<main class='col-md-4'>
<div class="card">
<div class='card-header'>{{ book.title }}</div>
<img src="{{ book.cover.url }}" class="card-img-top" >
<div class="card-body">
<p class="card-text">Published: {{ book.publication_date }}</p>
</div>
<div class='card-footer'>
{% if in_cart %}
<a href="{% url 'cart:remove-from-cart' %}" class='btn btn-default btn-block'>
Remove from Cart
</a>
{% else %}
<a href="{% url 'cart:add-to-cart' book.slug %}"class="btn btn-warning btn-block">
Add to Cart
</a>
{% endif %}
</div>
</div>
</main>
<aside class='col-md-8'>
<div class="card" style="width: 18rem;">
<div class="card-header">
Chapters
</div>
<ul class="list-group list-group-flush">
{% for chapter in book.chapter_set.all %}
<li class="list-group-item"> <a href="{{ chapter.get_absolute_url }}">
{{ chapter.chapter_number }}.{{ chapter.title }}
</a></li>
{% endfor %}
</ul>
</div>
</aside>
</div>
{% endblock %}
这是django项目的Models.py,但是我得到的错误是操作错误说:没有这样的列:shopping_cart_order.user_id 我卡住了,如果你知道什么,我将不胜感激。 林不知道它的模型,但我已经运行了命令: python manage.py makemigrations,python manage.py migration,python manage.py runserver