Defining models
A model is a Python class that represents a database table in Django. We can define models inside our app's models.py file. Here's an example model for a simple blog:
python
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
In this example, we define a Post model that has three fields: title, content, and created_at. The title and content fields are defined as CharField and TextField respectively, while the created_at field is defined as a DateTimeField with the auto_now_add option set to True. This means that the field will be automatically set to the current date and time when a new Post object is created.
No comments:
Post a Comment