Django authentication and social-sign on with django-allauth

How to add a user relationship to a Django model

Want more?

This lesson for enrolled students only. Join the course to unlock it!

You can see the code changes implemented in this lecture below.

If you have purchased the course in a different platform, you still have access to the code changes per lecture here on Teclado. The lecture video and lecture notes remain locked.
Join course for $15

New files

app/migrations/0003_article_creator_alter_article_word_count.py
# Generated by Django 5.1.3 on 2024-11-15 12:11

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('app', '0002_auto_20241113_1653'),
    ]

    operations = [
        migrations.AddField(
            model_name='article',
            name='creator',
            field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='articles', to=settings.AUTH_USER_MODEL),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='article',
            name='word_count',
            field=models.IntegerField(blank=True, default=''),
        ),
    ]

Modified files

app/models.py
--- 
+++ 
@@ -1,4 +1,5 @@
 import re
+from django.conf import settings
 from django.db import models
 from django.contrib.auth.models import AbstractUser

@@ -25,6 +26,7 @@
     )
     created_at = models.DateTimeField(auto_now_add=True)
     updated_at = models.DateTimeField(auto_now=True)
+    creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="articles")

     def save(self, *args, **kwargs):
         text = re.sub(r"<[^>]*>", "", self.content).replace("&nbsp;", " ")