Show furnitures in a different way considering django principle











up vote
4
down vote

favorite
1












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 %}









share|improve this question
















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.



















    up vote
    4
    down vote

    favorite
    1












    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 %}









    share|improve this question
















    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.

















      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      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 %}









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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.
























          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.






          share|improve this answer





















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            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

























            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.






            share|improve this answer

























              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.






              share|improve this answer























                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 2 at 0:11









                J_H

                4,387130




                4,387130






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'