22.5 Adding an Author to Blog Posts

Given that we are allowing specific users to write blog posts, it would be nice if we identified which blog post was written by which user. We can connect our Post to the User model exactly as if we had never customized it, as shown in Example 22.54.

Example 22.54: Project Code

blog/models.py in 9515ba8ede

 1   from django.conf import settings  .       ... 12   class Post(models.Model):  .       ... 18       author = models.ForeignKey( 19           settings.AUTH_USER_MODEL, 20           related_name='blog_posts')

Hopefully, our dependency on the use of get_user_model() and relations built using the setting.AUTH_USER_MODEL is a lot clearer than before. We won’t always know which ...

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.