Skip to content

Commit

Permalink
Starting Module 5: Managers and QuerySets
Browse files Browse the repository at this point in the history
  • Loading branch information
codesensei-courses committed Apr 26, 2024
1 parent 4f43c35 commit 92897af
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 16 deletions.
12 changes: 6 additions & 6 deletions carved_rock/carved_rock/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@
]

# Log all SQL
# LOGGING = {
# "version": 1,
# "handlers": {"console": {"class": "logging.StreamHandler"}},
# "loggers": {"django.db.backends": {"level": "DEBUG"}},
# "root": {"handlers": ["console"]},
# }
LOGGING = {
"version": 1,
"handlers": {"console": {"class": "logging.StreamHandler"}},
"loggers": {"django.db.backends": {"level": "DEBUG"}},
"root": {"handlers": ["console"]},
}

# Crispy
CRISPY_TEMPLATE_PACK = "bootstrap5"
Binary file modified carved_rock/db.sqlite3
Binary file not shown.
Binary file added carved_rock/static/media/blue_zmbcW7G.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions carved_rock/store/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import admin

from .models import Product

from .models import Product, ProductImage, Category

admin.site.register(Product)
admin.site.register(ProductImage)
admin.site.register(Category)
17 changes: 17 additions & 0 deletions carved_rock/store/migrations/0002_product_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.2 on 2024-02-21 20:25
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('store', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='product',
name='description',
field=models.TextField(default='', blank=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 5.0.2 on 2024-02-21 21:53

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('store', '0002_product_description'),
]

operations = [
migrations.AddField(
model_name='product',
name='sku',
field=models.CharField(default='', max_length=20, unique=True, verbose_name='Stock Keeping Unit'),
preserve_default=False,
),
migrations.AlterField(
model_name='product',
name='description',
field=models.TextField(blank=True, default=''),
),
migrations.AlterField(
model_name='product',
name='stock_count',
field=models.IntegerField(null=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.0.2 on 2024-02-22 13:53

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('store', '0003_product_sku_alter_product_description_and_more'),
]

operations = [
migrations.AlterField(
model_name='product',
name='stock_count',
field=models.IntegerField(help_text='How many items are currently in stock', null=True),
),
migrations.CreateModel(
name='ProductImage',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(upload_to='')),
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='store.product')),
],
),
]
21 changes: 21 additions & 0 deletions carved_rock/store/migrations/0005_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.0.2 on 2024-02-27 20:08

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('store', '0004_alter_product_stock_count_productimage'),
]

operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('products', models.ManyToManyField(to='store.product')),
],
),
]
24 changes: 23 additions & 1 deletion carved_rock/store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,27 @@

class Product(models.Model):
name = models.CharField(max_length=100)
stock_count = models.IntegerField()
stock_count = models.IntegerField(null=True,
help_text="How many items are currently in stock")
price = models.DecimalField(max_digits=6, decimal_places=2)
description = models.TextField(default="", blank=True)
sku = models.CharField(verbose_name="Stock Keeping Unit", max_length=20, unique=True)

def __str__(self):
return self.name


class ProductImage(models.Model):
image = models.ImageField()
product = models.ForeignKey('Product', on_delete=models.CASCADE)

def __str__(self):
return str(self.image)


class Category(models.Model):
name = models.CharField(max_length=100)
products = models.ManyToManyField('Product')

def __str__(self):
return self.name
12 changes: 5 additions & 7 deletions carved_rock/store/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from django.http import HttpResponse
from django.shortcuts import render

from .models import Product


def category_view(request, name):
# products = Product.objects.filter(category__name=name)
#
# return render(request, "store/category.html",
# {'products': products,
# 'category_name': name})
return HttpResponse("Category model not defined yet - see module 4")
products = Product.objects.filter(category__name=name)

return render(request, "store/category.html",
{'products': products,
'category_name': name})

0 comments on commit 92897af

Please sign in to comment.