db Flashcards

1
Q

The way select_related works is it

A

creates a join on the foreign key columns so you return all the data together in one query. (Imagine it nested)

select_related only works on single value fields: foreign key and one to one.

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

Using select_related and prefetch related does not

A

change how you access the foreign fields.

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

To use prefetch_related on a reverse foreign key relationship, type

A

ModelName.objects.all().prefetch_related(‘model_name_set’)

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

The way prefetch_related works is it

A

does a separate lookup for each relationship, and does the ‘joining’ in python.

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

To log queries to the console, use

A

pip install django-queryinspect

MIDDLEWARE = [
‘qinspect.middleware.QueryInspectMiddleware’,

QUERY_INSPECT_ENABLED = True

LOGGING = {
    'version': 1,
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'qinspect': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly