Created
March 13, 2024 23:23
-
-
Save huogerac/5152b5e29e3885593b9af5460e861958 to your computer and use it in GitHub Desktop.
carrinho de compra no Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django.urls import reverse | |
from django.contrib.auth import get_user_model | |
class Categoria(models.Model): | |
nome = models.CharField(max_length=32) | |
class Produto(models.Model): | |
categoria = models.ForeignKey(Categoria, on_delete=models.CASCADE) | |
descricao = models.CharField(max_length=128) | |
slug = models.SlugField(max_length=512) | |
valor = models.DecimalField(max_digits=12, decimal_places=2) | |
criado_em = models.DateTimeField(auto_now_add=True) | |
imagem = models.ImageField(upload_to='img/') | |
def __str__(self): | |
return f'{self.name}' | |
def get_absolute_url(self): | |
return reverse('produto:categoria', args=(self.slug,)) | |
class Carrinho(models.Model): | |
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE) | |
produtos = models.ManyToManyField(Produto, through='CarrinhoItem') | |
class CarrinhoItem(models.Model): | |
produto = models.ForeignKey(Produto, on_delete=models.CASCADE) | |
carrinho = models.ForeignKey(Carrinho, on_delete=models.CASCADE) | |
quantidade = models.IntegerField(default=1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment