Answer the question
In order to leave comments, you need to log in
How to write a command correctly?
Hello. There are two models
from django.db import models
import django
from shop.models import Product
from django.contrib.auth.models import User
from datetime import datetime
class Order(models.Model):
nickname = models.ForeignKey(User, on_delete=models.CASCADE, default=False)
email = models.EmailField()
postal_code = models.CharField(max_length=20)
city = models.CharField(max_length=100)
street = models.CharField(max_length=100, default="s")
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
paid = models.BooleanField(default=False)
class Meta:
ordering = ('-created',)
verbose_name = 'Заказ'
verbose_name_plural = 'Заказы'
def __str__(self):
return 'Order {}'.format(self.id)
def get_total_cost(self):
return sum(item.get_cost() for item in self.items.all())
class OrderItem(models.Model):
order = models.ForeignKey(Order, related_name='items')
product = models.ForeignKey(Product, related_name='order_items')
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
def my_orders(request):
"""Renders the about page."""
orders = Order.objects.filter(nickname=request.user)
assert isinstance(request, HttpRequest)
return render(
request,
'app/my_orders.html', {'orders': orders,}
)
orders = Order.objects.filter(nickname=request.user)
{% for order in orders %}
in the template. orders = Order.objects.filter(nickname=request.user)
order_item = OrderItem.objects.filter(что вот тут должно быть написано?
Or should I move in a different direction?
Answer the question
In order to leave comments, you need to log in
It seems so, but read about the connections
f = Order.orderItem_set.all()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question