0
0
Djangoframework~5 mins

Through model for extra fields on M2M in Django - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a through model in Django's many-to-many relationships?
A through model is a custom intermediate model that Django uses to store extra fields on a many-to-many relationship between two models.
Click to reveal answer
beginner
How do you specify a through model in a Django ManyToManyField?
You specify the through model by passing the model class name to the 'through' argument in the ManyToManyField, like: ManyToManyField('OtherModel', through='ThroughModel').
Click to reveal answer
intermediate
Why would you use a through model instead of a simple ManyToManyField?
You use a through model when you want to add extra information (fields) to the relationship itself, like a date or status, which a simple ManyToManyField cannot store.
Click to reveal answer
intermediate
In a through model, what fields must you include to link the two related models?
You must include two ForeignKey fields, each pointing to one of the related models, to connect them through the intermediate model.
Click to reveal answer
advanced
How do you add or access extra fields on a many-to-many relationship using a through model?
You create or query instances of the through model directly to set or get extra fields, instead of using the simple add() or remove() methods on the ManyToManyField.
Click to reveal answer
What argument do you use in ManyToManyField to specify a custom through model?
Aon_delete
Brelated_name
Cthrough
Ddb_table
Which of these is NOT true about a through model?
AIt replaces the related models themselves.
BIt must have ForeignKeys to both related models.
CIt can store extra fields about the relationship.
DIt allows more control over the many-to-many data.
How do you add a relationship with extra fields using a through model?
ACreate an instance of the through model directly.
BUse the add() method on the ManyToManyField.
CUse the remove() method on the ManyToManyField.
DUse the clear() method on the ManyToManyField.
If you don't specify a through model, what does Django do?
AIt requires you to define ForeignKeys manually.
BIt creates an automatic intermediate table without extra fields.
CIt disables the many-to-many relationship.
DIt raises an error.
Which field type is used in a through model to link to the related models?
ACharField
BOneToOneField
CManyToManyField
DForeignKey
Explain how to create a through model in Django to add extra fields on a many-to-many relationship.
Think about how to connect two models with extra info stored in between.
You got /3 concepts.
    Describe how you would add a new relationship with extra data using a through model in Django.
    Remember, you don't use add() on the ManyToManyField when extra fields exist.
    You got /4 concepts.