EngineerSpock
EngineerSpock

Reputation: 2675

Connecting Django's Admin Users to Model

Let's say I have a two models:

class Member(models.Model):
    nickname = models.CharField(max_length=128)
    email = models.EmailField()
    avatar = models.ImageField(upload_to='photos/')

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f'Member {self.nickname}'

class Dashboard(models.Model):
    title = models.CharField(max_length=128)
    owner = models.ForeignKey(Member, on_delete=models.CASCADE)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

Here I create a distinct model for tracking members who can edit dashboards. Can I use Django users for that instead, avoiding creation of my own models?

Upvotes: 0

Views: 30

Answers (1)

JPG
JPG

Reputation: 88629

Yes, you can.

Just use settings.AUTH_USER_MODEL as your related reference as,

from django.conf import settings


class Dashboard(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    # rest of your models

Note: You can also import the built-in User model from django.contrib.auth.models as @ruddra mentioned, But, there will be errors if you were already extended the auth model.

Upvotes: 1

Related Questions