Django Model increment

Hey there! Is there a way in Django that I can create a model that have a field that I can trigger that field to increment the number value everytime I send a query to it?

For example:

Jackie have a row in a table
The row have two fields name and count
Name = Jackie and count is an integer that will increase everytime I make a query to it

Every time I make a query out to “Jackie” via Django, it will take the count value of the count column and increase it by 1

So if Jackie count value is currently 12, once I make another query to the name Jackie it needs to increase to 13

Is that possible in Django model, or must I do the calculation in Django view?

This may seem like an easy question, but it’s really not.

The simple answer is “no”, you can’t effectively do that within Django. Not in any of the ways you identify.

In part, it depends upon what you mean by “send a query to it”.

Specifically, you don’t ever “send a query” to a table. You send a query to the database. The database engine determines what tables and fields need to be referenced.

It becomes even more complex when you account for relationships between tables, or tables being used in Subqueries or Unions.

In Django terms, you also need to factor in that Django can cache query results, such that two identical queries in the same view don’t necessarily result in two separate queries being issued to the database - the second can be retrieved from cache without a query being issued.

So in part, you need to really understand what you’re trying to accomplish by this. What is your actual objective here?

In the Django tutorial you can build a polls app with a voting mechanism. Each time a post request is sent, it increments the number of votes field in the model instance by 1.

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the question voting form.
        return render(request, 'polls/detail.html', {
            'question': question,
            'error_message': "You didn't select a choice.",
        selected_choice.votes += 1
        return HttpResponseRedirect(reverse('polls:results', args=(,)))
<form action="{% url 'polls:vote' %}" method="post">
    {% csrf_token %}
        <legend><h1>{{ question.question_text }}</h1></legend>
        {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
        {% for choice in question.choice_set.all %}
            <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ }}">
            <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
        {% endfor %}
    <input type="submit" value="Vote">

Is this principle something you could use?

I gave a prime example in the question that I posed.


Stevenson Gerard Eustache

Similar, but not in the view. Maybe it can be done with signals


Stevenson Gerard Eustache

That is one example, yes. You haven’t yet addressed all the ancillary issues that I listed in my previous post.

What exactly are you trying to determine by attempting to do this? We might be able to more accurately answer your question if we understood what the underlying objective is.