Orm Theory
Orm Theory
class CommonInfo(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
class Meta:
abstract = True
class Student(CommonInfo):
student_id = models.CharField(max_length=10)
```
- **Use Case:** Use ABC when you have common fields for multiple models.
2. **Multi-table Inheritance:**
- **Definition:** Each model in the inheritance chain has its own database
table.
- **Example:**
```python
class Person(models.Model):
name = models.CharField(max_length=100)
class Employee(Person):
employee_id = models.CharField(max_length=10)
```
- **Use Case:** Use this to keep each model's data in separate tables.
3. **Proxy Models:**
- **Definition:** Create a different representation of an existing model without
creating a new table.
- **Example:**
```python
class MyModel(models.Model):
name = models.CharField(max_length=100)
class MyModelProxy(MyModel):
class Meta:
proxy = True
ordering = ['name']
```
- **Use Case:** Use proxy models to change behavior without altering the
original model.
class Dog(Animal):
breed = models.CharField(max_length=50)
```
- **Use Case:** Use when base models need to be instantiated and have their own
tables.
class Meta:
ordering = ['name']
verbose_name = 'Product'
verbose_name_plural = 'Products'
```
- **Attributes:**
- `ordering`: Specifies default ordering.
- `verbose_name`: A human-readable name for the model.
- `verbose_name_plural`: Plural name for the model.
These concepts help in organizing and managing data models effectively in Django.
Optimizing Django queries and reducing the number of database hits means improving
how efficiently your application retrieves data from the database. By using
select_related and prefetch_related, you can minimize the number of separate
database queries needed to fetch related objects. This results in faster data
retrieval and less load on the database, leading to better performance of your
application.
select_related
Use Case: When you have a ForeignKey or OneToOneField relationship and you want to
fetch related objects in a single query.
select_related
Use Case: When you have a ForeignKey or OneToOneField relationship and you want to
fetch related objects in a single query.
RAW Method
The raw method in Django allows you to run custom SQL queries directly within your
Django application. This can be useful when you need to perform complex queries
that are not easily achievable with Django's ORM.
Caution
While using raw queries, be careful as they bypass Django's built-in protections.
This might introduce security risks, like SQL injection, and compatibility issues
with different databases. It's best to use them only when necessary and rely on
Django's ORM for most queries.
In this context, "bypass" means to avoid or skip over. When we say that using raw
SQL queries bypasses Django's built-in protections, it means that these queries do
not use the usual safeguards provided by Django's ORM. These safeguards help
prevent issues like SQL injection attacks and ensure compatibility with different
database systems. Using raw SQL queries directly interacts with the database
without these safety checks, which can lead to security vulnerabilities and other
problems if not handled carefully.
In Django's ORM, a cursor is an interface to interact with the database directly.
It allows you to execute SQL queries, fetch results, and navigate through the data.
While Django handles most database interactions automatically, using a cursor
provides more control, especially for complex queries.
Redirect:
Reverse Lazy:
Purpose: Lazily resolves a URL when it's actually needed, not when the code is
loaded.
Usage: Useful for resolving URLs in places where URLs need to be specified but the
URLconf is not yet loaded.
Reverse:
Purpose: Sends the user to a different URL after an action, such as form
submission.
Usage: Used to navigate users to another page or view after processing.
exclude()
In Django's ORM (Object-Relational Mapping), the exclude() method is used to
construct a query that
excludes certain objects from the result set based on specific conditions
AbstractUser:
Purpose: Provides a user model with common fields like username, email, password,
etc.
Use Case: When you need a user model similar to Django's default but want to add
extra fields.
AbstractBaseUser:
Purpose: Offers more flexibility by requiring you to define the user model fields
and methods.
Use Case: When you need a highly customized user model.
Summary:
If you need a user model without a username but with an email as the unique
identifier, you can use AbstractBaseUser and define this behavior manually.