Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the reason why Django has SmallIntegerField?

I'm wonder why it's provided. The field is database dependent, doesn't that make it totally unreliable to use?

I want to store birth year in a model, kinda like

class Person(models.Model):   name = models.CharField(max_length=256)   born = models.IntegerField() 

Of course this requires very little space, it should always be 4 "characters" long, so a PositiveSmallIntegerField would maybe fit, but why should I choose it instead of normal IntegerField?

like image 230
odinho - Velmont Avatar asked Oct 04 '09 14:10

odinho - Velmont


People also ask

What is SmallIntegerField in Django?

SmallIntegerField is a integer number represented in Python by a int instance. This field is like a IntegerField and supports values from -32768 to 32767. The default form widget for this field is a NumberInput when localize is False or TextInput otherwise.

What is AutoField in Django?

According to documentation, An AutoField is an IntegerField that automatically increments according to available IDs. One usually won't need to use this directly because a primary key field will automatically be added to your model if you don't specify otherwise.

What is Django ForeignKey model?

ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases. ForeignKey is defined within the django. db. models. related module but is typically referenced from django.

What is primary key in Django?

By default, Django adds an id field to each model, which is used as the primary key for that model. You can create your own primary key field by adding the keyword arg primary_key=True to a field. If you add your own primary key field, the automatic one will not be added.


1 Answers

Performance on many RDBMs can be heavily dependent on row size. While a "purist" approach might say that the application should be completely independent of the underlying data structure, over many rows improvements like using a smaller integer could shave gigabytes off table size, which makes more of the table fit in memory, which drastically improves performance. It's the Brief part of the ABCs.

I would use a small integer like this for say, a primary key on a table that would always have <100 rows, especially when this is stored as a foreign key in a table I expect to grow very large. While the size is implementation defined, it's safe to assume that it is greater than 127 at the very least.

like image 102
Todd Gardner Avatar answered Sep 23 '22 19:09

Todd Gardner