Show furnitures in a different way considering django principle
up vote
4
down vote
favorite
I want in my homepage to show the furnitures items in a following way:
Newly Arrived Furnitures(upto 10)
Featured Furnitures
Free shipping Furnitures (upto 10)
For this I tried to go with the principle of Fat Models, Thin View
. Here is my code
class FurnitureQuerySet(models.QuerySet):
def active(self):
return self.filter(is_active=True)
def featured(self):
return self.active().filter(is_featured=True)
def new_arrival(self, max_furnitures):
return self.active().order_by('-id')[:max_furnitures]
def free_shipping(self, max_furnitures):
return self.active().filter(is_free_shipping=True)[:max_furnitures]
class FurnitureManager(models.Manager):
def get_queryset(self):
return FurnitureQuerySet(self.model, using=self._db)
def all(self):
return self.get_queryset().active()
def featured(self):
return self.get_queryset().featured()
def new_arrival(self, max_furnitures):
return self.get_queryset().new_arrival(max_furnitures)
def free_shipping(self, max_furnitures):
return self.get_queryset().free_shipping(max_furnitures)
def get_by_id(self, slug):
qs = self.get_queryset().filter(slug=slug)
if qs.count() == 1:
return qs.first()
return None
class Furniture(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
manufacturer = models.ForeignKey(Manufacturer, blank=True, null=True)
slug = models.SlugField(max_length=200, unique=True)
is_active = models.BooleanField(
default=True, help_text='Furniture is available for listing and sale')
is_featured = models.BooleanField(
default=False, help_text='Promote this furniture on main pages')
is_free_shipping = models.BooleanField(
default=False, help_text='No shipping charges')
timestamp = models.DateTimeField(auto_now_add=True)
objects = FurnitureManager()
def home(request):
categories = Category.objects.root_nodes()
furnitures = Furniture.objects.new_arrival(10)
featured_furnitures = Furniture.objects.featured()
shipping_free_furnitures = Furniture.objects.free_shipping(10)
context = {
'categories': categories,
'furnitures': furnitures,
'featured': featured_furnitures,
'shipping_free': shipping_free_furnitures,
}
return render(request, 'home.html', context)
The template will be:
<h1>Newly Arrival</h1>
{% for new_furniture in furnitures %}
{{ new_furniture.name }}
{% endfor %}
<h1>Featured</h1>
{% for featured_furniture in featured %}
{{ featured_furniture.name }}
{% endfor %}
python python-3.x django
bumped to the homepage by Community♦ 18 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
4
down vote
favorite
I want in my homepage to show the furnitures items in a following way:
Newly Arrived Furnitures(upto 10)
Featured Furnitures
Free shipping Furnitures (upto 10)
For this I tried to go with the principle of Fat Models, Thin View
. Here is my code
class FurnitureQuerySet(models.QuerySet):
def active(self):
return self.filter(is_active=True)
def featured(self):
return self.active().filter(is_featured=True)
def new_arrival(self, max_furnitures):
return self.active().order_by('-id')[:max_furnitures]
def free_shipping(self, max_furnitures):
return self.active().filter(is_free_shipping=True)[:max_furnitures]
class FurnitureManager(models.Manager):
def get_queryset(self):
return FurnitureQuerySet(self.model, using=self._db)
def all(self):
return self.get_queryset().active()
def featured(self):
return self.get_queryset().featured()
def new_arrival(self, max_furnitures):
return self.get_queryset().new_arrival(max_furnitures)
def free_shipping(self, max_furnitures):
return self.get_queryset().free_shipping(max_furnitures)
def get_by_id(self, slug):
qs = self.get_queryset().filter(slug=slug)
if qs.count() == 1:
return qs.first()
return None
class Furniture(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
manufacturer = models.ForeignKey(Manufacturer, blank=True, null=True)
slug = models.SlugField(max_length=200, unique=True)
is_active = models.BooleanField(
default=True, help_text='Furniture is available for listing and sale')
is_featured = models.BooleanField(
default=False, help_text='Promote this furniture on main pages')
is_free_shipping = models.BooleanField(
default=False, help_text='No shipping charges')
timestamp = models.DateTimeField(auto_now_add=True)
objects = FurnitureManager()
def home(request):
categories = Category.objects.root_nodes()
furnitures = Furniture.objects.new_arrival(10)
featured_furnitures = Furniture.objects.featured()
shipping_free_furnitures = Furniture.objects.free_shipping(10)
context = {
'categories': categories,
'furnitures': furnitures,
'featured': featured_furnitures,
'shipping_free': shipping_free_furnitures,
}
return render(request, 'home.html', context)
The template will be:
<h1>Newly Arrival</h1>
{% for new_furniture in furnitures %}
{{ new_furniture.name }}
{% endfor %}
<h1>Featured</h1>
{% for featured_furniture in featured %}
{{ featured_furniture.name }}
{% endfor %}
python python-3.x django
bumped to the homepage by Community♦ 18 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I want in my homepage to show the furnitures items in a following way:
Newly Arrived Furnitures(upto 10)
Featured Furnitures
Free shipping Furnitures (upto 10)
For this I tried to go with the principle of Fat Models, Thin View
. Here is my code
class FurnitureQuerySet(models.QuerySet):
def active(self):
return self.filter(is_active=True)
def featured(self):
return self.active().filter(is_featured=True)
def new_arrival(self, max_furnitures):
return self.active().order_by('-id')[:max_furnitures]
def free_shipping(self, max_furnitures):
return self.active().filter(is_free_shipping=True)[:max_furnitures]
class FurnitureManager(models.Manager):
def get_queryset(self):
return FurnitureQuerySet(self.model, using=self._db)
def all(self):
return self.get_queryset().active()
def featured(self):
return self.get_queryset().featured()
def new_arrival(self, max_furnitures):
return self.get_queryset().new_arrival(max_furnitures)
def free_shipping(self, max_furnitures):
return self.get_queryset().free_shipping(max_furnitures)
def get_by_id(self, slug):
qs = self.get_queryset().filter(slug=slug)
if qs.count() == 1:
return qs.first()
return None
class Furniture(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
manufacturer = models.ForeignKey(Manufacturer, blank=True, null=True)
slug = models.SlugField(max_length=200, unique=True)
is_active = models.BooleanField(
default=True, help_text='Furniture is available for listing and sale')
is_featured = models.BooleanField(
default=False, help_text='Promote this furniture on main pages')
is_free_shipping = models.BooleanField(
default=False, help_text='No shipping charges')
timestamp = models.DateTimeField(auto_now_add=True)
objects = FurnitureManager()
def home(request):
categories = Category.objects.root_nodes()
furnitures = Furniture.objects.new_arrival(10)
featured_furnitures = Furniture.objects.featured()
shipping_free_furnitures = Furniture.objects.free_shipping(10)
context = {
'categories': categories,
'furnitures': furnitures,
'featured': featured_furnitures,
'shipping_free': shipping_free_furnitures,
}
return render(request, 'home.html', context)
The template will be:
<h1>Newly Arrival</h1>
{% for new_furniture in furnitures %}
{{ new_furniture.name }}
{% endfor %}
<h1>Featured</h1>
{% for featured_furniture in featured %}
{{ featured_furniture.name }}
{% endfor %}
python python-3.x django
I want in my homepage to show the furnitures items in a following way:
Newly Arrived Furnitures(upto 10)
Featured Furnitures
Free shipping Furnitures (upto 10)
For this I tried to go with the principle of Fat Models, Thin View
. Here is my code
class FurnitureQuerySet(models.QuerySet):
def active(self):
return self.filter(is_active=True)
def featured(self):
return self.active().filter(is_featured=True)
def new_arrival(self, max_furnitures):
return self.active().order_by('-id')[:max_furnitures]
def free_shipping(self, max_furnitures):
return self.active().filter(is_free_shipping=True)[:max_furnitures]
class FurnitureManager(models.Manager):
def get_queryset(self):
return FurnitureQuerySet(self.model, using=self._db)
def all(self):
return self.get_queryset().active()
def featured(self):
return self.get_queryset().featured()
def new_arrival(self, max_furnitures):
return self.get_queryset().new_arrival(max_furnitures)
def free_shipping(self, max_furnitures):
return self.get_queryset().free_shipping(max_furnitures)
def get_by_id(self, slug):
qs = self.get_queryset().filter(slug=slug)
if qs.count() == 1:
return qs.first()
return None
class Furniture(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
manufacturer = models.ForeignKey(Manufacturer, blank=True, null=True)
slug = models.SlugField(max_length=200, unique=True)
is_active = models.BooleanField(
default=True, help_text='Furniture is available for listing and sale')
is_featured = models.BooleanField(
default=False, help_text='Promote this furniture on main pages')
is_free_shipping = models.BooleanField(
default=False, help_text='No shipping charges')
timestamp = models.DateTimeField(auto_now_add=True)
objects = FurnitureManager()
def home(request):
categories = Category.objects.root_nodes()
furnitures = Furniture.objects.new_arrival(10)
featured_furnitures = Furniture.objects.featured()
shipping_free_furnitures = Furniture.objects.free_shipping(10)
context = {
'categories': categories,
'furnitures': furnitures,
'featured': featured_furnitures,
'shipping_free': shipping_free_furnitures,
}
return render(request, 'home.html', context)
The template will be:
<h1>Newly Arrival</h1>
{% for new_furniture in furnitures %}
{{ new_furniture.name }}
{% endfor %}
<h1>Featured</h1>
{% for featured_furniture in featured %}
{{ featured_furniture.name }}
{% endfor %}
python python-3.x django
python python-3.x django
edited Dec 13 '17 at 0:44
Sᴀᴍ Onᴇᴌᴀ
7,84561749
7,84561749
asked Dec 13 '17 at 0:41
beginner boy
211
211
bumped to the homepage by Community♦ 18 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 18 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Do take care to run flake8
over the source, and follow its advice.
Array slicing [:max_furnitures]
is idiomatic and nice. But if you have a thousand or a million items, you will prefer to use SQL limit
, so you don't retrieve a ton of items from the DB only to discard them. You will get a different execution plan from the backend DB if you expose limit 10
to its query optimizer.
I see four different max_furnitures parameters. Now maybe this is perfectly nice and we prefer to leave it as is. But also consider introducing it as an attribute used by constructor.
Looks good.
Ship it.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Do take care to run flake8
over the source, and follow its advice.
Array slicing [:max_furnitures]
is idiomatic and nice. But if you have a thousand or a million items, you will prefer to use SQL limit
, so you don't retrieve a ton of items from the DB only to discard them. You will get a different execution plan from the backend DB if you expose limit 10
to its query optimizer.
I see four different max_furnitures parameters. Now maybe this is perfectly nice and we prefer to leave it as is. But also consider introducing it as an attribute used by constructor.
Looks good.
Ship it.
add a comment |
up vote
0
down vote
Do take care to run flake8
over the source, and follow its advice.
Array slicing [:max_furnitures]
is idiomatic and nice. But if you have a thousand or a million items, you will prefer to use SQL limit
, so you don't retrieve a ton of items from the DB only to discard them. You will get a different execution plan from the backend DB if you expose limit 10
to its query optimizer.
I see four different max_furnitures parameters. Now maybe this is perfectly nice and we prefer to leave it as is. But also consider introducing it as an attribute used by constructor.
Looks good.
Ship it.
add a comment |
up vote
0
down vote
up vote
0
down vote
Do take care to run flake8
over the source, and follow its advice.
Array slicing [:max_furnitures]
is idiomatic and nice. But if you have a thousand or a million items, you will prefer to use SQL limit
, so you don't retrieve a ton of items from the DB only to discard them. You will get a different execution plan from the backend DB if you expose limit 10
to its query optimizer.
I see four different max_furnitures parameters. Now maybe this is perfectly nice and we prefer to leave it as is. But also consider introducing it as an attribute used by constructor.
Looks good.
Ship it.
Do take care to run flake8
over the source, and follow its advice.
Array slicing [:max_furnitures]
is idiomatic and nice. But if you have a thousand or a million items, you will prefer to use SQL limit
, so you don't retrieve a ton of items from the DB only to discard them. You will get a different execution plan from the backend DB if you expose limit 10
to its query optimizer.
I see four different max_furnitures parameters. Now maybe this is perfectly nice and we prefer to leave it as is. But also consider introducing it as an attribute used by constructor.
Looks good.
Ship it.
answered Apr 2 at 0:11
J_H
4,387130
4,387130
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f182638%2fshow-furnitures-in-a-different-way-considering-django-principle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown