34👍
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]
8👍
Every Django model already has an auto-generated primary key:
id = models.AutoField(primary_key=True)
It seems you are trying to duplicate an already existing behavior, just use the object primary key.
- [Django]-Substring in a django template?
- [Django]-PHP Frameworks (CodeIgniter, Yii, CakePHP) vs. Django
- [Django]-POST jQuery array to Django
6👍
I, too, came across this problem, my instance of it was customer.number
which was relative to the customers Store
. I was tempted to use something like:
# Don't do this:
class Customer(models.Model):
# store = ...
number = models.IntegerField(default=0)
def save(self, *args, **kwargs):
if self.number == 0:
try:
self.number = self.store.customer_set.count() + 1
else:
self.number = 1
super(Customer, self).save(*args, **kwargs)
The above can cause several problems: Say there were 10 Customers, and I deleted customer number 6. The next customer to be added would be (seemingly) the 10th customer, which would then become a second Customer #10. (This could cause big errors in get()
querysets)
What I ended up with was something like:
class Store(models.Model):
customer_number = models.IntegerField(default=1)
class Customer(models.Model):
store = models.ForeignKey(Store)
number = models.IntegerField(default=0)
def save(self, *args, **kwargs):
if self.number == 0:
self.number = self.store.customer_number
self.store.number += 1
self.store.save()
super(Customer, self).save(*args, **kwargs)
PS:
You threw out several times that you wanted this field filled in “before“. I imagine you wanted it filled in before saving so that you can access it. To that I would say: this method allows you to access store.customer_number
to see the next number to come.
- [Django]-How do I remove Label text in Django generated form?
- [Django]-How to test "render to template" functions in django? (TDD)
- [Django]-Django 2 – How to register a user using email confirmation and CBVs?
0👍
You have errors in code, that’s why you can’t import it:
from django.db import models
class WhatEver(models.Model):
number = models.IntegerField('Just a Field', default=0)
and Yuval A is right about auto-incrementing: you don’t even need to declare such a field. Just use the pk
or id
, they mean the same unless there’s a composite pk in the model:
> w = Whatever(number=10)
> w
<Whatever object>
> w.id
None
> w.save()
> w.id
1
[update] Well, I haven’t tried a callable as a default. I think if you fix these errors, it must work.
- [Django]-Django apps aren't loaded yet when using asgi
- [Django]-How do I remove Label text in Django generated form?
- [Django]-Cannot access django app through ip address while accessing it through localhost