Add models for market app

This commit is contained in:
jhyns 2023-05-01 20:49:25 +09:00
parent 57717c9cd1
commit 2b2805d889
2 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,29 @@
from django.contrib import admin
from .models import Brand, Product, ProductColor, ProductStorage, Post, Image
admin.site.register(Brand)
admin.site.register(Product)
admin.site.register(ProductColor)
admin.site.register(ProductStorage)
admin.site.register(Image)
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = (
"product",
"color",
"storage",
"price",
"text",
"author",
"status",
"written_at",
)
list_filter = ("status", "written_at")
search_fields = ("product", "text")
date_hierarchy = "written_at"
ordering = ("status", "written_at")
raw_id_fields = ("author",)

View File

@ -0,0 +1,65 @@
from django.db import models
from user.models import User
class Brand(models.Model):
name = models.CharField(max_length=20)
image = models.ImageField(upload_to="brands", blank=True, null=True)
def __str__(self):
return self.name
class ProductColor(models.Model):
color = models.CharField(max_length=20)
def __str__(self):
return self.color
class ProductStorage(models.Model):
storage = models.CharField(max_length=20)
def __str__(self):
return self.storage
class Product(models.Model):
name = models.CharField(max_length=50)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
image = models.ImageField(upload_to="products", blank=True, null=True)
color = models.ManyToManyField("ProductColor", blank=True)
storage = models.ManyToManyField("ProductStorage", blank=True)
def __str__(self):
return self.name
class Post(models.Model):
STATUS_CHOICES = (
("s", "selling"),
("r", "reserved"),
("d", "done"),
)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
color = models.ForeignKey(ProductColor, on_delete=models.CASCADE)
storage = models.ForeignKey(ProductStorage, on_delete=models.CASCADE)
price = models.IntegerField()
text = models.TextField()
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
status = models.CharField(
max_length=1, choices=STATUS_CHOICES, default="s"
)
written_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.product.name
class Image(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
image = models.ImageField(upload_to="images", blank=True, null=True)
def __str__(self):
return self.post.product.name