Django Advanced Flashcards

1
Q

How to use file-based sessions

A

To use the same, you need to set the SESSION_ENGINE settings to “django.contrib.sessions.backends.file”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is mixin

A

Mixin is a type of multiple inheritances wherein you can combine behaviors and attributes of more than one parent class. It provides us with an excellent way to reuse code from multiple classes. One drawback of using these mixins is that it becomes difficult to analyze what a class is doing and which methods to override in case of its code being too scattered between multiple classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Django Field Class

A
'Field' refers to an abstract class that represents a column in the database table. 
The Field class is just a subclass of RegisterLookupMixin. In Django, these fields are used to create database tables (db_types()) which are used to map Python types to the database using get_prep_value() and the other way round using from_db_value() method. Therefore, fields are fundamental pieces in different Django APIs such as models and querysets
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why is permanent redirection not a good option

A

Permanent redirection is used only when you don’t want to lead visitors to the old URLs. The response of the permanent redirections is cached by the browser so when you try to redirect to something else it will cause issues. Since this is a browser-side operation if your user wants to move to a new page it will load the same page

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Difference between Django OneToOneField and ForeignKey Field

A

Both of them are of the most common types of fields used in Django. The only difference between these two is that ForeignKey field consists of on_delete option along with a model’s class because it’s used for many-to-one relationships while on the other hand, the OneToOneField, only carries out a one-to-one relationship and requires only the model’s class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you combine multiple QuerySets in a View

A

Initially, Concatenating QuerySets into lists is believed to be the easiest approach. Here’s an example of how to do that:
from itertools import chain
result_list = list(chain(model1_list, model2_list, model3_list))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to get a particular item in the Model

A
ModelName.objects.get(id=”term”)
Note: If there are no results that match the query, get() will raise a DoesNotExist exception. If more than one item matches the given get() query. In this case, it’ll raise MultipleObjectsReturned, which is also an attribute of the model class itself
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to obtain the SQL query from the queryset

A

print(queryset.query)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the ways to customize the functionality of the Django admin interface

A

There are multiple ways to customize the functionality of the Django admin interface. You can piggyback on top of an add/change form that’s automatically generated by Django, you can add JavaScript modules using the js parameter. This parameter is basically a list of URLs that point to the JavaScript modules that are to be included in your project within a tag. You can also write views for the admin if you want

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Difference between select_related and prefetch_related

A

Though both the functions are used to fetch the related fields on a model but their functioning is bit different from each other. In simple words, select_related uses a foreign key relationship, i.e. using join on the query itself while on the prefetch_related there is a separate lookup and the joining on the python side. Let’s try to illustrate this via an example:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain Q objects in Django ORM

A

Q objects are used to write complex queries, as in filter() functions just AND the conditions while if you want to OR the conditions you can use Q objects. Let’s see an example:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are Django exceptions

A

In addition to the standard Python exceptions, Django raises of its own exceptions.List of the exceptions by Django (https://docs.djangoproject.com/en/3.1/ref/exceptions/)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly