Facebook PageView

Model Fields with examples

Written by Parvatiandsons Team

Django provides various model fields that you can use to define the attributes of your models. Here's a list of some common model fields along with examples of how to use them:


1. CharField:

   - Used for short text fields like names, titles, and tags.

   - Example: Storing a person's name.



name = models.CharField(max_length=100)


2. TextField:

   - Used for longer text fields like descriptions, comments, or content.

   - Example: Storing a blog post's content.



content = models.TextField()


3. IntegerField:

   - Used for storing integer values.

   - Example: Storing a user's age.



age = models.IntegerField()


4. FloatField:

   - Used for storing floating-point numbers.

   - Example: Storing a product's price.



price = models.FloatField()


5. BooleanField:

   - Used for storing boolean (True/False) values.

   - Example: Storing whether a user has subscribed to a newsletter.



subscribed_to_newsletter = models.BooleanField(default=False)


6. DateField:

   - Used for storing date values.

   - Example: Storing a book's publication date.



 publication_date = models.DateField()


7. DateTimeField:

   - Used for storing date and time values.

   - Example: Storing the date and time when a user's account was created.



account_created_at = models.DateTimeField(auto_now_add=True)


8. EmailField:

   - Specifically designed for storing email addresses.

   - Example: Storing a user's email address.



email = models.EmailField()


9. URLField:

   - Used for storing URLs.

   - Example: Storing a website's URL.



website_url = models.URLField()


10. ImageField:

    - Used for storing image files.

    - Example: Storing a user's profile picture.



 profile_picture = models.ImageField(upload_to='profile_pictures/')


11. ForeignKey:

    - Used to define a many-to-one relationship with another model.

    - Example: Storing the author of a blog post.



author = models.ForeignKey(User, on_delete=models.CASCADE)


12. ManyToManyField:

    - Used to define a many-to-many relationship with another model.

    - Example: Storing the categories of a product.



categories = models.ManyToManyField(Category)


13. FileField:

    - Used for storing any type of file.

    - Example: Storing user-uploaded files.



uploaded_file = models.FileField(upload_to='uploads/')


14. AutoField:

    - A special field that automatically generates a unique integer for each new record.

    - Example: Automatically generating primary keys for records.



id = models.AutoField(primary_key=True)


15. SlugField:

    - Used for generating URL slugs from a text field.

    - Example: Storing slugs for SEO-friendly URLs.


slug = models.SlugField(unique=True)


These are some of the commonly used model fields in Django. You can choose the appropriate field type based on the data you want to store in your database. Additionally, you can specify various options and attributes for each field to customize its behavior and validation rules.

  •     Django Models and it's use
  • Custom Validators of Model Field