Skip to content

Commit

Permalink
feat (backend) : Added Product Models (#87)
Browse files Browse the repository at this point in the history
* Merge branch develop into Priyansh61/product-model

* Done requested changes
  • Loading branch information
Priyansh61 authored Jul 22, 2023
1 parent 9f051c8 commit 18dbbd7
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 1 deletion.
3 changes: 3 additions & 0 deletions backend/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'django_filters',
'core',
'accounts',
'products',
]

REST_FRAMEWORK = {
Expand Down Expand Up @@ -137,3 +138,5 @@
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

REST_FRAMEWORK = {}
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
4 changes: 3 additions & 1 deletion backend/core/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
path('admin/', admin.site.urls),
path('api/token/', include('auth.urls')),
path('api/accounts/', include('accounts.urls')),
]
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Empty file added backend/products/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions backend/products/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.contrib import admin
from .models import Product,ProductVariation,ProductColor,ProductSize,ProductImage

# Register your models here.

class ProductAdmin(admin.ModelAdmin) :
list_display = ("name","description","category",'primary_variant',"created_at","updated_at")
fields = ("name","description","category","primary_variant",)

class ProductVariationAdmin(admin.ModelAdmin) :
list_display = ("Product","ProductSize","ProductColor","price","quantity")

class ProductColorAdmin(admin.ModelAdmin) :
list_display = ("color",)

class ProductSizeAdmin(admin.ModelAdmin) :
list_display = ("size",)

class ProductImageAdmin(admin.ModelAdmin) :
list_display = ("product_variation_id","image")


admin.site.register(Product,ProductAdmin)
admin.site.register(ProductVariation,ProductVariationAdmin)
admin.site.register(ProductColor,ProductColorAdmin)
admin.site.register(ProductSize,ProductSizeAdmin)
admin.site.register(ProductImage,ProductImageAdmin)



6 changes: 6 additions & 0 deletions backend/products/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ProductsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'products'
73 changes: 73 additions & 0 deletions backend/products/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from django.db import models

# Create your models here.

category_choices = [
('clothing','Clothing'),
('rsvp','RSVP'),
]

class Product(models.Model):
name = models.CharField(max_length=200)
description = models.TextField(max_length=600,
blank=True)
category = models.CharField(max_length=100,
choices=category_choices)
primary_variant = models.OneToOneField(
'ProductVariation',
on_delete=models.CASCADE,
default=None,
null=True,
blank=True,)

# Need to add Primary Variant and seller Id
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return self.name


class ProductVariation(models.Model):
Product = models.ForeignKey(Product,
on_delete=models.CASCADE,
related_name='product_variations')
ProductColor = models.ForeignKey('ProductColor',
on_delete=models.CASCADE)
ProductSize = models.ForeignKey('ProductSize',
on_delete=models.CASCADE)
price = models.IntegerField()
quantity = models.IntegerField()
cover_image = models.ForeignKey('ProductImage',
on_delete=models.CASCADE,
default=None, null=True,
blank=True,)
is_active = models.BooleanField(default=True)

def __str__(self):
return f'{self.Product.name} {self.ProductColor.color} {self.ProductSize.size}'


class ProductColor(models.Model):
color = models.CharField(max_length=100, unique=True)

def __str__(self):
return self.color

class ProductSize(models.Model):
size = models.CharField(max_length=100, unique=True)

def __str__(self):
return self.size


class ProductImage(models.Model):
product_variation_id = models.ForeignKey(ProductVariation,
on_delete=models.CASCADE,
related_name='product_images')
image = models.ImageField(upload_to='product_images')

required = ['image']

def __str__(self):
return f'{self.product_variation_id.Product.name} {self.product_variation_id.ProductColor.color} {self.product_variation_id.ProductSize.size}'
3 changes: 3 additions & 0 deletions backend/products/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions backend/products/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pytest==7.4.0
pytest-django==4.5.2
python-dotenv==1.0.0
pytz==2023.3
Pillow==10.0.0
sqlparse==0.4.4
tomli==2.0.1
typing_extensions==4.6.3
Expand Down

0 comments on commit 18dbbd7

Please sign in to comment.