Django Advanced Flashcards
How to use file-based sessions
To use the same, you need to set the SESSION_ENGINE settings to “django.contrib.sessions.backends.file”
What is mixin
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
What is Django Field Class
'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
Why is permanent redirection not a good option
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
Difference between Django OneToOneField and ForeignKey Field
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 can you combine multiple QuerySets in a View
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 to get a particular item in the Model
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 to obtain the SQL query from the queryset
print(queryset.query)
What are the ways to customize the functionality of the Django admin interface
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
Difference between select_related and prefetch_related
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:
Explain Q objects in Django ORM
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:
What are Django exceptions
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/)