Sobes.tech
Middle

What connection would you use to extend the standard user model?

sobes.tech AI

Answer from AI

I would use a OneToOneField relationship.

This is the optimal way because it:

  • Ensures uniqueness: Each user record in the standard model will correspond to exactly one record in your extended model.
  • Easy access: Allows easy access to extended model data directly through the standard user object and vice versa.
  • Data integrity: Avoids duplication of information and simplifies management of related data.

Example:

# models.py

from django.contrib.auth.models import User
from django.db import models

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(blank=True)
    website = models.URLField(blank=True)

    def __str__(self):
        return self.user.username