InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What are Django exceptions? |
|
Answer» 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/)
Important Resources: Django Projects Node.js VS Django Flask Vs Django
|
|
| 2. |
Explain Q objects in Django ORM? |
|
Answer» 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: from django.db import modelsfrom django.db.models import Q>> objects = Models.objects.get( Q(tag__startswith='Human'), Q(category=’Eyes’) | Q(category=’Nose’))```Query ExecutedSELECT * FROM MODEL WHERE tag LIKE ‘Human%’ AND (category=’Eyes’ OR category=’Nose’)``` |
|
| 3. |
Difference between select_related and prefetch_related? |
|
Answer» 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: from django.db IMPORT modelsclass Country(models.Model): country_name = models.CharField(max_length=5)CLASS State(models.Model): state_name = models.CharField(max_length=5) country = model.ForeignKey(Country)>> states = State.objects.select_related(‘country’).all()>> for state in states:… PRINT(state.state_name) ```Query ExecutedSELECT state_id, state_name, country_name FROM State INNER JOIN Country ON (State.country_id = Country.id)```>> country = Country.objects.prefetch_related(‘state’).get(id=1)>> for state in country.state.all():… print(state.state_name)```Query ExecutedSELECT id, country_name FROM country WHERE id=1;SELECT state_id, state_name WHERE State WHERE country_id IN (1);``` |
|
| 4. |
What are the ways to customize the functionality of the Django admin interface? |
|
Answer» 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 <script> tag. You can also write views for the admin if you want. |
|
| 5. |
How to obtain the SQL query from the queryset? |
|
Answer» PRINT(queryset.query) |
|
| 6. |
How to get a particular item in the Model? |
|
Answer» ModelName.objects.get(id=”term”) |
|
| 7. |
How can you combine multiple QuerySets in a View? |
|
Answer» Initially, CONCATENATING QuerySets into lists is believed to be the EASIEST APPROACH. Here’s an EXAMPLE of how to do that: |
|
| 8. |
Difference between Django OneToOneField and ForeignKey Field? |
|
Answer» 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. |
|
| 9. |
Why is permanent redirection not a good option? |
|
Answer» 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. |
|
| 10. |
What is Django Field Class? |
|
Answer» 'Field' refers to an abstract class that represents a column in the database table. |
|
| 11. |
What is mixin? |
|
Answer» 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. |
|