Django storing mobile number, what field to use? - django

In my models I need to store a mobile number in the following format 447182716281. What field should I use? Does Django have anything to support this?
example
mobile = models.IntegerField(max_length=12)

Phone numbers must be CharFields. Integer field will not preserve leading 0, +, and spacing.

I think is a interesting question since it really depends on the problem modeling, CharField works fine, but have a look at this:
ORM tricks

There is a regex field in form validation. In model use just CharField.

On using models.IntegerField(default=0)
for larger number it gives error
Ensure this value is less than or equal to 2147483647.
So better way to use would be.
BigIntegerField
A 64-bit integer, much like an IntegerField except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807. The default form widget for this field is a TextInput.

with PostgreSQL IntegerField is not working properly so it's better to use CharField with Django.
Number=models.CharField(max_length=12)

Related

What is the default range for IntegerFields on Django?

Such a simple question, but can't seem to find anything in the documentation about it.
For example, can integers be negative?
There is none. Any integer is valid. Negative is definitely fine. But the IntegerField is not limited by default. #16747 closed Bug (wontfix) IntegerField could validate too large numbers to work with PostgreSQL and MySQL be default was a request to fix this, but it was denied.
Keep in mind that an IntegerField is not necessarily connected to a particular database field. It usually is, in which case the recommendation is to add max_value and min_value to match your database field requirements.
If you're talking about models.IntegerField then there is indeed a range for that and it is mentioned in the documentation.
An integer. Values from -2147483648 to 2147483647 are safe in all
databases supported by Django.
It uses MinValueValidator and MaxValueValidator to validate the input
based on the values that the default database supports.
If you're talking about forms.IntegerField, then the only way to validate it is to pass a parameter of max_value and min_value for that.
Validates that the given value is an integer. Uses MaxValueValidator
and MinValueValidator if max_value and min_value are provided.

Django CharField with no max length

I have a frontend that will be sending bas64 images, I will put those very large strings on a variable and send it to a form. What kind of FormField can I use?
The regular CharFields need a max_length. Since it is a very large string, I don't know the length it could have.
Maybe TextField could be the right choice.

Django model field max_length 3x varchar size?

I just realized that with a legacy table I'm using in a django app that a varchar(5) field (for example) is rendered in python as a models.CharField(max_length=15) field. This 3x size for the max length is very consistent across many other fields.
Why? or more importantly if I changed the django definition to be models.CharField(max_length=5) would I break anything?
It is probably a manual error by someone who tried to write models.
No. It doesn't break anything if you change it to 5. Not only that, you should change it to 5, so your form validation itself will take care of that length where you have that field.

How to determine the maximum integer the model can handle?

"What is the biggest integer the model field that this application instance can handle?"
We have sys.maxint, but I'm looking for the database+model instance. We have the IntegerField, the SmallIntegerField, the PositiveSmallIntegerField, and a couple of others beside. They could all vary between each other and each database type.
I found the "IntegerRangeField" custom field example here on stackoverflow. Might have to use that, and guess the lowest common denominator? Or rethink the design I suppose.
Is there an easy way to work out the biggest integer an IntegerField, or its variants, can cope with?
It depends on your database backend.
Use ./manage.py sql your_app_name to check generated types for your DB-columns, and look through your database documentation for type ranges.
For MySQL: http://dev.mysql.com/doc/refman/5.1/en/numeric-types.html
For PostgreSQL: http://www.postgresql.org/docs/8.1/static/datatype.html#DATATYPE-TABLE
Can't be easily done. Just set a constant and use that.
MAX_SMALL_INT = 32767
position = models.PositiveSmallIntegerField(default=MAX_SMALL_INT)

Appropriateness of a Django Textfield in a Model

I have a field in a model that I want users to feel like they can write an arbitrary amount of text in. Django provides a CharField and a TextField in the models. I assume that the difference is that one of them is a char(max_length) and the other is a varchar internally.
I am tempted to use the TextField, but since it doesn't respect max_length, I am somewhat wary of someone dumping loads of data into it and DOSing my server. How should I deal with this?
Thanks!
Fields in model only represent the way data is stored in database.
You can very easily enforce maximum length in form which will validate users' input. Like this.
class InputForm(forms.Form):
text = forms.CharField(max_length=16384, widget=forms.TextArea)
...
This will make sure the maximum length user can successfully enter is 16k.