Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 858025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:29:17+00:00 2026-05-15T08:29:17+00:00

Snippet taken from this question from django.db.models import F … MyModel.objects.filter(id=…).update(hit_count=F(hit_count)+1) It was suggested

  • 0

Snippet taken from this question

from django.db.models import F
...
MyModel.objects.filter(id=...).update(hit_count=F(hit_count)+1)

It was suggested to put in the middleware and I read up a bit on middleware but would greatly appreciate if someone could point out what they would do in this situation. Using my Bug model as an example it has a unique slug field and the pk.

Here’s my model:

class Bug( models.Model ):
    name = models.CharField( max_length=100 )
    slug = models.SlugField(unique=True)
    excerpt = models.TextField()
    excerpt_markdown = models.TextField( editable=False, blank=True )
    summary = models.TextField()
    summary_markdown = models.TextField(editable=False, blank=True)
    #workaround = models.TextField()
    #workaround_markdown = models.TextField(editable=False, blank=True)
    date_added = models.DateTimeField()
    poster = models.ForeignKey(User)
    tags_string = TagField()

    class Meta:
    ordering = ['name']

    def __unicode__(self):
    return self.name

    def get_absolute_url(self):
    return '/bugs/%s/' % self.slug

    def save( self, force_insert=False, force_update=False ):
    self.summary_markdown = markdown(  self.summary  )
    self.excerpt_markdown = markdown ( self.excerpt )
    #self.workaround_markdown = markdown(  self.workaround )
    super( Bug, self ).save( force_insert, force_update )

Links are viewed through /bugs/(slug). I’ve yet to add the new column but I imagine it’s just hit_counter = models.IntegerField()

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:29:18+00:00Added an answer on May 15, 2026 at 8:29 am

    You have three options:

    1. Place the code in a middleware so that it is universally available in your template through the RequestContext.

    2. Place the code in a decorator (essentially just a python function that wraps another function and adds some functionality) so that you can decide where you want this “hit counting” code to be applied.

    3. Place the code in a custom template tag that will increment the counter every time a template with the tag is rendered.

    Option three (template tag) is the most complicated, but the most suited to the actual purpose since it can render the counter and increment it in a single piece of code. Option 1 (middleware) is the easiest, but also the least flexible/reusable, etc.

    Middleware and template tags are both well documented in Django’s docs. You’ll get a lot more out of working through them than you will out of getting handed a code snippet here on SO.

    Decorators are a basic part of Python since v2.4, and are worth learning, but not the solution I’d choose here. They’re a powerful tool and a Google search (or even a search on SO) will give you tons of information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This snippet (taken from this question ) compiles fine with g++ (as seen), so
Hey all, the following is a snippet of code taken from the unix ptx
I have a very basic question. Lets take this snippet: #include <stdio.h> void foo(void)
I am reading modern C++ design. Following is the text snippet taken from typeinfo
I know from reading this Stackoverflow question that the complier will look at your
I am curious, what average is obtained from this code snippet? The accumulator is
Take this code snippet for example: window.location.href = 'mysite.htm#images'; Already being on the site
There's probably a Google search that'll answer this question but for the life of
I tried to phrase this as a generic question but realized I don't know
Take this snippet out of a controller, for example: public ActionResult Login() { if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.