/    /  Django Fields

Django Fields

In this tutorial we will understand Fields and Field types.

Fields:

The most important part in creating a model is Fields. You need to have a clear understanding about what all fields that are required to start creating an application.

These fields are none other than the database fields or table column names. Fields are specified as class attributes.

Note: While creating fields you must that they dont conflict with models API ( clean, save, delete)

From django.db import models
class Courses(models.Model):
    course_name = models.CharField(max_length=50)
    course_description = models.CharField(max_length=50)
    course_duration = models.IntegerField()

class Students(models.Model):
    student_name = models.CharField(max_length=100)
    age = models.IntegerField()  
    email = models.EmailField()
    mobile = models.PositiveIntegerField()

Field types:

Below are the different Field types that you can use based on your requirement while creating the models.

1. AutoField:

An Integer Field that automatically increments.

2. BigAutoField:

A 64-bit integer, much like an AutoField except that it is guaranteed to fit numbers from 1 to 9223372036854775807.

3. BigIntegerField:

A 64-bit integer, much like an IntegerField except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807.

4. BinaryField:

A field to store raw binary data.

5. BooleanField:

A true/false field. The default form widget for this field is a CheckboxInput.

6. CharField:

A string field, for small- to large-sized strings.For large amounts of text, use TextField.

7. DateTimeField:

A date, represented in Python by a datetime.date instance.

8. DecimalField:

A fixed-precision decimal number, represented in Python by a Decimal instance.

9. DurationField:

A field for storing periods of time.

10. EmailField:

A CharField that checks that the value is a valid email address.

11. FileField:

A file-upload field.

12. FilePathField:

A CharField whose choices are limited to the filenames in a certain directory on the filesystem.

13. FloatField:

A floating-point number represented in Python by a float instance.
14. ImageField:

It inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image.

15. GenericIPAddressField:

An IPv4 or IPv6 address, in string format (e.g. 192.0.2.30 or 2a02:42fe::4).

16. IntegerField:

It is an integer field. Values from -2147483648 to 2147483647 are safe in all databases supported by Django.

17. NullBooleanField:

Like a BooleanField, but allows NULL as one of the options.

18. PositiveIntegerField:

Like an IntegerField, but must be either positive or zero (0). Values from 0 to 2147483647 are safe in all databases supported by Django.

19. SmallIntegerField:

It is like an IntegerField, but only allows values under a certain (database-dependent) point.

20. TextField:

A large text field. The default form widget for this field is a Textarea.

21. TimeField:

A time, represented in Python by a datetime.time instance.

22. URLField:

A CharField for a URL, validated by URLValidator.