7👍
Since Django 3.2 the type of implicit primary key can be controlled with the DEFAULT_AUTO_FIELD
setting (documentation). So, there is no need anymore to override primary keys in all your models.
#This setting will change all implicitly added primary keys to BigAutoField
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
Note that starting with Django 3.2 new projects are generated with DEFAULT_AUTO_FIELD
set to BigAutoField
(release notes).
21👍
Django now has a BigAutoField built in if you are using Django 1.10:
https://docs.djangoproject.com/en/1.10/ref/models/fields/#bigautofield
- [Django]-Django : Table doesn't exist
- [Django]-Redis Python – how to delete all keys according to a specific pattern In python, without python iterating
- [Django]-Django – Where are the params stored on a PUT/DELETE request?
18👍
Inspired by lf*gundes but with a small but important correction:
class BigAutoField(fields.AutoField):
def db_type(self, connection): # pylint: disable=W0621
if 'mysql' in connection.__class__.__module__:
return 'bigint AUTO_INCREMENT'
return super(BigAutoField, self).db_type(connection)
add_introspection_rules([], [r"^a\.b\.c\.BigAutoField"])
Notice instead of extending BigIntegerField, I am extending AutoField. This is an important distinction. With AutoField, Django will retrieve the AUTO INCREMENTed id from the database, whereas BigInteger will not.
One concern when changing from BigIntegerField to AutoField was the casting of the data to an int in AutoField.
Notice from Django’s AutoField:
def to_python(self, value):
if value is None:
return value
try:
return int(value)
except (TypeError, ValueError):
msg = self.error_messages['invalid'] % str(value)
raise exceptions.ValidationError(msg)
and
def get_prep_value(self, value):
if value is None:
return None
return int(value)
It turns out this is OK, as verified in a python shell:
>>> l2 = 99999999999999999999999999999
>>> type(l2)
<type 'long'>
>>> int(l2)
99999999999999999999999999999L
>>> type(l2)
<type 'long'>
>>> type(int(l2))
<type 'long'>
In other words, casting to an int will not truncate the number, nor will it change the underlying type.
- [Django]-Django-admin.py makemessages not working
- [Django]-How to create table during Django tests with managed = False
- [Django]-Django model manager objects.create where is the documentation?
14👍
NOTE: This answer as modified, according to Larry’s code. Previous solution extended fields.BigIntegerField, but better to extend fields.AutoField
I had the same problem and solved with following code:
from django.db.models import fields
from south.modelsinspector import add_introspection_rules
class BigAutoField(fields.AutoField):
def db_type(self, connection):
if 'mysql' in connection.__class__.__module__:
return 'bigint AUTO_INCREMENT'
return super(BigAutoField, self).db_type(connection)
add_introspection_rules([], ["^MYAPP\.fields\.BigAutoField"])
Apparently this is working fine with south migrations.
- [Django]-Django: how does manytomanyfield with through appear in admin?
- [Django]-How to get a particular attribute from queryset in Django in view?
- [Django]-How to use "AND" in a Django filter?
- [Django]-Django: create Index: non-unique, multiple column
- [Django]-Django select_for_update cannot be used outside of a transaction
- [Django]-Django Admin linking to related objects
3👍
As stated before you could alter the table afterwards. That is a good solution.
To do that without forgetting, you can create a management module under your application package and use the post_syncdb signal.
https://docs.djangoproject.com/en/dev/ref/signals/#post-syncdb
This can cause django-admin.py flush to fail. But it is still the best alternative I know.
- [Django]-Pre-populate an inline FormSet?
- [Django]-Django rest framework – filtering for serializer field
- [Django]-Django SMTPAuthenticationError
2👍
I also had the same problem. Looks like there is no support for BigInteger auto fields in django.
I’ve tried to create some custom field BigIntegerAutoField but I faced a problem with south migration system (south couldn’t create sequence for my field).
After giving a try couple of different approaches I decided to follow Matthew’s advice and do alter table (e.g. ALTER TABLE table_name ALTER COLUMN id TYPE bigint;
in postgre)
Would be great to have solution supported by django (like built in BigIntegerAutoField) and south.
- [Django]-Generating a Random Hex Color in Python
- [Django]-How to create an empty queryset and to add objects manually in django
- [Django]-Choose test database?