class Person(models.Model):
name = models.CharField(max_length=100)
class Article(models.Model):
content = models.TextField()
class Product(models.Model):
quantity = models.IntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2) # Precise price storage
rating = models.FloatField()
FloatField
is a Django model field used to store floating-point numbers. It’s a wrapper around Python’s float type.from decimal import Decimal
n = Decimal('0.1')
print(n)
class Task(models.Model):
completed = models.BooleanField(default=False)
class Event(models.Model):
event_date = models.DateField()
event_datetime = models.DateTimeField(auto_now_add=True)
Note: auto_now_add
automatically adds the date. If you want to change the timezone used by Django’s auto_now_add
feature from UTC to another timezone, you need to configure Django’s timezone settings appropriately in your settings file (settings.py
):
Set Timezone in settings.py
: Ensure your desired timezone is set in your Django project settings:
TIME_ZONE = 'America/New_York'
Enable Timezone Support: Ensure that Django’s timezone support is enabled by setting USE_TZ
to True
in your settings.py
file:
USE_TZ = True
class Document(models.Model):
doc_file = models.FileField(upload_to='documents/')
image_file = models.ImageField(upload_to='images/')
doc_file
stores uploaded documents.image_file
stores uploaded images.class Document(models.Model):
id = models.BigAutoField(primary_key=True)
class Document(models.Model):
id = models.BigIntegerField(primary_key=True)
Examples:
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE)