Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Django 2 Web Development Cookbook - Third Edition

You're reading from  Django 2 Web Development Cookbook - Third Edition

Product type Book
Published in Oct 2018
Publisher
ISBN-13 9781788837682
Pages 544 pages
Edition 3rd Edition
Languages
Authors (2):
Jake Kronika Jake Kronika
Profile icon Jake Kronika
Aidas Bendoraitis Aidas Bendoraitis
Profile icon Aidas Bendoraitis
View More author details
Toc

Table of Contents (14) Chapters close

Preface 1. Getting Started with Django 2.1 2. Database Structure and Modeling 3. Forms and Views 4. Templates and JavaScript 5. Customizing Template Filters and Tags 6. Model Administration 7. Security and Performance 8. Django CMS 9. Hierarchical Structures 10. Importing and Exporting Data 11. Bells and Whistles 12. Testing and Deployment 13. Other Books You May Enjoy

Creating a model mixin to handle generic relations

Aside from normal database relationships, such as a foreign-key relationship or a many-to-many relationship, Django has a mechanism to relate a model to an instance of any other model. This concept is called generic relations. For each generic relation, there is a content type of the related model that is saved, as well as the ID of the instance of that model.

In this recipe, we will look at how to abstract the creation of generic relations in the model mixins.

Getting ready

For this recipe to work, you will need to have the contenttypes app installed. It should be in the INSTALLED_APPS directory, by default, as shown in the following code:

# settings.py or config/base.py
INSTALLED_APPS...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}