Django Flashcards

1
Q

what is context variable lookup?

A

it’s a mechanism in django that allows us to insert variables in our templates. These variables are added to the context and rendered in the view.

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

How does DTL handle invalid variables?

A

returns an empty string.

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

what is reverse URL resolution in django?

A

it’s a way to get the absolute url, we can use either use the reverse( ) method or in the template url template tag, and in the model class get_absolute_url( ).

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

What is the functionality of django.contrib.auth app?

A

It’s a built-in Django application. It gives authentication and authorization functionality out of the box.

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

describe laziness in django.

A

The concept of laziness refers to when a function call is not immediately executed until its return value is accessed. The best example of this in Django is the querysets. The queryset object doesn’t actually make a call to the database until the queryset is evaluated. Laziness improves performance in our Django applications because we take fewer trips to the DB.

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

how does Django deal with static file optimization?

A

Django makes use of the ManifestStaticFileStorage to optimize static files. it does this by taking advantage of browser caching. when we collectstatic it appends an md5 hash of the contents of each static to its name. You can cache this file. on the browser it’s one less asset to load, we can also cache this for pages that still make reference to it.

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

What is ManifestStaticFileStorage and how does it work?

A

It is one of the ways Django optimizes static files. it stores the file name it handles by appending the md5 hash of the files content to the filename. ex css/styles.css would also be saved as css/55e7cbb9a48.css

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

what is the STATIC_ROOT setting in Django?

A

it’s the absolute path to the directory where we collectstatic files to be served.

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

What is the STATIC_URL setting in django?

A

It’s the url to use when referring to static files located in the static root.

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

What is the purpose of the ManifestStorageFile class?

A

It allows developers to serve old static files by pages that still reference them. it also helps with load time by taking advantage of the browser caching.

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

what is collectstatic?

A

collectstatic scans the directories in the setting STATICFILES_DIRS. All static files found are then
collected to the directory at STATIC_ROOT.

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

what is pagination?

A

Pagination is a way to split a large data set into smaller data subsets that you can iterate over the pages/subsets. The pages are provided as per user request. Advantage of pagination is improved performance, less bandwith and overhead for each request.

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

How do you implement pagination in django?

A

Use the Paginator class or module.

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

what are signals in django?

A

Signals is a mechanism in django that allows developers to communicate events between apps. There are many types of signals in django. The ones i am familiar with are the model based signals. ex pre_save, post_save, pre_delete etc.

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

What are some django signals?

A

Model signals: pre_save, post_sav, pre_delete.

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

What is the advantage of signals?

A

We use signals to transmit messages between applications. The messages will give notification of an event. Signals are useful when several applications in your project are interested in the same mutual event.

17
Q

What is the ORM

A

object-relational mapping, The ORM maps your model attributes to your relational database table fields. it also provides a convenient API to communicate with your DB using a python object so developers don’t need to write any SQL.

18
Q

What are some advantages of Django’s database migrations?

A

1) provides versioning for your database schemas.

2) keeps your database and models in sync.

19
Q

What is the difference between Django.TestCase and unittest.TestCase?

A

Django.TestCase is a subclass of unittest.TestCase. The TestCase instance encloses all operations into a database transaction that it rolls back at the end. This is how it does to keep the database clean for the following tests.

20
Q

What is the difference between setUpTestData and set up?

A

You should use setUpTestData for the shared data among your tests as it runs once per class and rolls back all transactions at the end of execution. setUp runs once per test useful if you need to run an operation once per test.

21
Q

What are some advantages of mixins?

A

The primary advantage is code reusability.

22
Q

Describe Mixins

A

Mixins make use of pythons multiple inheritance and extends functionality for classed based views. It allows you to combine attributes and methods of multiple parents through the use of multiple inheritance.

23
Q

What are some disadvantages of mixins?

A

It can lead to issues with code readability if the mixins have deep inheritance trees.

24
Q

What are some differences between class-based views and function-based views?

A

1) in classed based views the code is organized around defining specific HTTP methods or providing generic view classes that do and extending functionality by either providing mixins or overriding methods and attributes. 2) function-based views, on the other hand, are organized using generic python functions that use conditional branching and renders a response. 3) Class-based views are python objects and function-based vies are regular python views. 4) Class-based views provide better reusability with the use of multiple inheritance and mixins. One isn’t better than the other it just depends on your problem.

25
Q

Describe Abstract models in Django.

A

Abstract models hold reusable information, that is inherited by other classes. No DB tables of its own are created.

26
Q

Describe Multi-Table inheritance in Django

A

In a multi-table inheritance each model in the hierarchy has a corresponding DB table of its own. The inheritance relationship introduces links between the child model and each of its parents (via an automatically-created OneToOneField).

27
Q

Describe Proxy models in Django.

A

when using multi-table inheritance proxy models provide python level behavior to models. When you create, delete, and update instances of the proxy model all the data will be saved as if you were using the original (non-proxied) model. The difference is that you can change things like the default model ordering or the default manager in the proxy, without having to alter the original.

28
Q

What are some disadvantages of the Django ORM?

A

It cannot perform highly complex queries easily and when it does isn’t the most efficient, you end up resorting to writing manual SQL. it doesn’t always perform DB queries and operations efficiently. an alternative is SQLAlchemy.

29
Q

Describe Django request-response cycle?

A

when a request comes in from the browser Django first attempts to resolve the URL by matching the incoming URL to one of the URL patterns defined in your urls.py if it finds a match it calls the view associated to that URL pattern. The view handles interactions with the DB by interacting with models and renders a response that is sent back to the user.

30
Q

What is the difference between select_related and prefetch related

A

Select related is used for one to one or one to many foreign key relationships. it combines tables by performing a SQL join. Prefetch_related is used for many to many relationships. it performs two queries. one to get the prefetch ids. and a second using WHERE in