Junior — Middle
Can you name the main methods provided by Django ORM for interacting with the database?
sobes.tech AI
Answer from AI
Django ORM provides many methods for convenient interaction with the database. The main ones are:
Model.objects.all()— returns all records of the model.Model.objects.filter(**kwargs)— filters records based on given conditions.Model.objects.get(**kwargs)— returns a single record matching the conditions (raises an exception if not found or multiple found).Model.objects.exclude(**kwargs)— excludes records matching the conditions.Model.objects.order_by('field')— sorts records by the specified field.Model.objects.create(**kwargs)— creates and saves a new record.Model.objects.update(**kwargs)— updates records (usually called through a QuerySet).Model.objects.delete()— deletes records.
Example:
# Get all users older than 18
adults = User.objects.filter(age__gt=18)
# Create a new user
User.objects.create(username='ivan', age=25)
This demonstrates the basic methods for interacting with the database using Django ORM.