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

The Archive Base Latest Questions

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

def __number(): # This line returns the number of the latest created object #

  • 0
def __number():
    # This line returns the number of the latest created object
    # as a "Factura" object in format "n/year"
    last = Factura.objects.filter(f_type__exact=False).latest('number')

    # We convert it into a string and split it to get only the first number
    spl = str(last).split('/')[0]

    # Convert it into integer so we can do math
    n = int(spl)

    # Get the current year
    y = date.today().strftime('%y')

    # If none return 1/year
    if n == None:
        return str(1) + '/' + str(y)

    # Else we increment the number in one.
    else:
        n = n + 1
        return str(n) + '/' + str(y)

What it does: It autogenerates a number in the format ‘1/year’ ‘2/year’ etc. If the user introduces other number, p.e. 564/10 the function follows it and the next will be 565/10.

Even if the user introduces p.e. 34/10 after the entry with 564/10 the function will follow the largest number.

Did I do this right or there’s a better way to do it?


Update:

def __number():
    current_year = date.today().strftime('%y')
    try:
        facturas_emmited = Factura.objects.filter(f_type__exact=False)
        latest_object = facturas_emmited.latest('number').__str__()
        first_number = int(latest_object.split("/")[0]) + 1
    except Factura.DoesNotExist:
        first_number = 1
    return '%s/%s' % (first_number, current_year)
  • 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:38:15+00:00Added an answer on May 15, 2026 at 2:38 pm

    This is really just the beginning, but I’d start by replacing some comments with self-documenting code.

    def __number():
        # "Factura" object in format "n/year"
        latest_object = Factura.objects.filter(f_type__exact=False).latest('number')
    
        # Better name can be available if you explain why the first number is important and what it means
        # Do Factura objects not have a __repr__ or __str__ method that you must cast it?
        first_number = int(str(latest_object).split('/')[0])
        current_year = date.today().strftime('%y')
        # Use "is None" rather than "== None"
        if first_number is None:
            return '1/%d' % current_year
        # No else needed because of return above
        # Why do we add 1 to first number? Comments should explain _why_, not how
        return '%d/%d' % (first_number + 1, current_year)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python module that defines a number of classes: class A(object): def
I have code: class Scene def initialize(number) @number = number end attr_reader :number end
My code is based on the methods described here and here . def fraction?(number)
I have a function which returns 3 numbers, e.g.: def numbers(): return 1,2,3 usually
Consider the following Scala code: package scala_java object MyScala { def setFunc(func: Int =>
So the situation is like this. Suppose we have: class Test(datetime): def a(self): return
I had to comment out the line below (example is from http://en.wikibooks.org/wiki/Clojure_Programming/Examples/Lazy_Fibonacci ) (def
I'm trying to unit test some code that looks like this: def main(): parser
I have this line in a useful Bash script that I haven't managed to
I was so pleased with myself getting this to work last night without any

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.