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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:16:12+00:00 2026-05-18T01:16:12+00:00

I’m trying to set up a project listing where when I set up a

  • 0

I’m trying to set up a project listing where when I set up a project it has a project number (example 10-1000). I want to be able to add a new project and have it append 1 to the project number, (example, next project # will be 10-1001). I am having a bit of trouble figuring out the first step.

here is my models.py

class Project(models.Model):
    client = models.ForeignKey(Clients, related_name='projects')
    created_by = models.ForeignKey(User, related_name='created_by')


    #general information
    proj_name = models.CharField(max_length=255, verbose_name='Project Name')
    quote = models.CharField(max_length=10, verbose_name='Quote #', unique=True)
    desc = models.TextField(verbose_name='Description')
    starts_on = models.DateField(verbose_name='Start Date')
    completed_on = models.DateField(verbose_name='Finished On')

    def __unicode__(self):
        return u'%s' % (self.proj_name) 

    #get the current status of the projectget_value_display
    def current_status(self):
        try:
            return self.status.all().order_by('-id')[:1][0]
        except:
            return None

My views.py showing the add

@login_required
def addProject(request):
    if request.method == 'POST':
        form = AddSingleProjectForm(request.POST)
        if form.is_valid():
            project = form.save(commit=False)
            project.created_by = request.user 
            today = datetime.date.today()
            project.quote = "%s-%s" % (str(today.year)[2:4], project.quote)
            project.save()
            project.status.create(
                    value = form.cleaned_data.get('status', None)
            )            
            return HttpResponseRedirect('/project/')
    else:
        form = AddSingleProjectForm()

    return render_to_response('project/addProject.html', {
    'form': form, 'user':request.user}, context_instance=RequestContext(request))

and my forms.py

class AddSingleProjectForm(ModelForm):
    status = forms.ChoiceField(choices=STATUS_CHOICES)
    def __init__(self, *args, **kwargs):
        super(AddSingleProjectForm, self).__init__(*args, **kwargs)
        self.fields['status'].initial = self.instance.current_status()

    class Meta:
        model = Project
        exclude = ('pre_quote', 'created_by')

    def save(self, force_insert=False, force_update=False, commit=True):
        f = super(AddSingleProjectForm, self).save(commit=False)
        if commit:
            f.save()
            print "form save method was called with commit TRUE"
        return f

Any suggestions would be greatly appreciated.
Thanks everyone!

  • 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-18T01:16:12+00:00Added an answer on May 18, 2026 at 1:16 am

    It looks to me that your “quote” is two things: a monotonically incrementing “project number”, and a year. Since that’s what they are, they have a regular structure. Exclude them from your form and view entirely, and create two new fields: “quote_year” and “quote_id.”

    Your save would then look something like this:

    def save(self, *args, **kwargs):
        if not self.quote_year and not self.quote_id:
            self.quote_year = datetime.date.today().year % 100
            quote_ids = self.objects.filter(quote_year = self.quote_year).order_by('-quote_id')
            self.quote_id = 1
            if quote_ids.exists():
                self.quote_id = quote_ids[0].quote_id + 1
        super(Project, self).save(*args, **kwargs)
    

    This basically tests to see if there are any quote_ids for the current year and, if not, initialize to 1, otherwise initialize to the highest quote_id plus 1.

    In order to save yourself grief rendering the quote ID again and again, add this to your model as well:

    @property
    def quote(self):
        return '%02d-%05d' % (self.quote_year, self.quote_id)
    

    My code assumes that you want the project part of the number to reset to zero for the first project of each year.

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

Sidebar

Related Questions

No related questions found

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.