Sobes.tech
Junior — Middle

How can a model object be defined and initialized within Django ORM?

sobes.tech AI

Answer from AI

In Django ORM, a model object is defined as a class that inherits from django.db.models.Model. Inside the class, model fields are described using Django field types.

Example of defining and initializing a model:

from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=10, decimal_places=2)

# Creating and saving a model object
product = Product(name='Coffee', price=250.00)
product.save()

Here, Product is the model, and product is an instance of the model that can be saved to the database by calling save().