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 892259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:01:26+00:00 2026-05-15T14:01:26+00:00

[ Update: Changed question title to be more specific] Sorry if I didn’t make

  • 0

[Update: Changed question title to be more specific]

Sorry if I didn’t make the question very well, I can’t figure how to do this:

class WhatEver():
    number = model.IntegerField('Just a Field', default=callablefunction)
...

Where callablefunction does this query:

from myproject.app.models import WhatEver

def callablefunction():
    no = WhatEver.objects.count()
    return no + 1

I want to automatically write the next number, and I don’t know how to do it.

I have errors from callablefunction stating that it cannot import the model, and I think there must be an easier way to do this. There’s no need even to use this, but I can’t figure how to do it with the pk number.

I’ve googled about this and the only thing I found was to use the save() method for auto incrementing the number… but I wanted to show it in the <textfield> before saving…

What would you do?

  • 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-15T14:01:26+00:00Added an answer on May 15, 2026 at 2:01 pm

    Got it! I hope this will help everyone that has any problems making a auto-filled and auto-incrementing field in django. The solution is:

    class Cliente(models.Model):
        """This is the client data model, it holds all client information. This
           docstring has to be improved."""
        def number():
            no = Cliente.objects.count()
            if no == None:
                return 1
            else:
                return no + 1
    
        clientcode = models.IntegerField(_('Code'), max_length=6, unique=True, \
        default=number)
    
        [... here goes the rest of your model ...]
    

    Take in care:

    • The number function doesn’t take any arguments (not even self)
    • It’s written BEFORE everything in the model
    • This was tested on django 1.2.1

    This function will automatically fill the clientcode field with the next number (i.e. If you have 132 clients, when you add the next one the field will be filled with clientcode number 133)

    I know that this is absurd for most of the practical situations, since the PK number is also auto-incrementing, but there’s no way to autofill or take a practical use for it inside the django admin.

    [update: as I stated in my comment, there’s a way to use the primary key for this, but it will not fill the field before saving]

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

Sidebar

Related Questions

First of all, I'm sorry I can't figure out a better title for this
UPDATE: Facebook's API has changed a lot since this question was posted. This question
Sorry about the question title, but I couldn't find a more appropriate way to
Ok, so that title probably doesn't explain my question well. Hopefully this makes sense.
Sorry, this is a bad question. I don't even know what the title should
UPDATE: I've changed the wording of the question. Previously it was a yes/no question
I changed hdd1\Content folder with JTAG console, how to restore and update this folder,
Not sure what the correct title for this question should be. I have the
I think the title for this question is probably wrong, but I'm not sure
My question is very similar to this one: Getting an error in jQuery when

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.