Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Andrew and Alexander_django quieries_solution #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion ormQueries/products/product_crud.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,86 @@
from .models import Product
from django.db.models import Q
from django.db.models import Avg, Max
from django.db.models.functions import Length



class ProductCrud:
@classmethod
def get_all_products(cls):
pass
return Product.objects.all()

@classmethod
def find_by_model(cls, model_name):
return Product.objects.get(model = model_name)

@classmethod
def last_record(cls):
return Product.objects.last()
#[e for e in Product.objects.all()][-1]

@classmethod
def by_rating(cls, rating):
return Product.objects.filter(rating = rating)

@classmethod
def by_rating_range(cls, rating_low, rating_high):
return Product.objects.filter(rating__range = (rating_low,rating_high))

@classmethod
def by_rating_and_color(cls, rating, color):
return Product.objects.filter(rating = rating, color = color)

@classmethod
def by_rating_or_color(cls, rating, color):
return Product.objects.filter(Q(rating = rating) | Q(color = color))

@classmethod
def no_color_count(cls):
return Product.objects.filter(color__isnull = True).count()

@classmethod
def below_price_or_above_rating(cls,below_price,above_rating):
return Product.objects.filter(Q(price_cents__lt = below_price) | Q(rating__gt = above_rating))

@classmethod
def ordered_by_category_alphabetical_order_and_then_price_decending(cls):
return Product.objects.order_by('category', '-price_cents')

@classmethod
def products_by_manufacturer_with_name_like(cls, input):
return Product.objects.filter(manufacturer__icontains = input)

@classmethod
def manufacturer_names_for_query(cls, input):
return Product.objects.filter(manufacturer__icontains = input).values_list('manufacturer',flat=True)

@classmethod
def not_in_a_category(cls, category):
return Product.objects.exclude(category = category)

@classmethod
def limited_not_in_a_category(cls, category, limit):
return Product.objects.exclude(category = category)[:limit]

@classmethod
def category_manufacturers(cls, category):
return Product.objects.filter(category = category).values_list('manufacturer' ,flat=True)

@classmethod
def average_category_rating(cls, category):
return Product.objects.filter(category = category).aggregate(Avg('rating'))

@classmethod
def greatest_price(cls):
return Product.objects.all().aggregate(Max('price_cents'))

@classmethod
def longest_model_name(cls):
return Product.objects.order_by(Length('model')).values_list('id', flat=True).last()

@classmethod
def ordered_by_model_length(cls):
return Product.objects.order_by(Length('model'))