Delete the django admin logs model

Hi,

I have created a new user authentication system using DRF. As part of this implementation, I have to run migrations that error because the django_admin_logs model already exists.

Other than losing the historic data, is it safe to delete and recreate from the new migration?

I’ve done this in dev and seems to be fine, but wasn’t sure if this is model is linked to anything else?

Thanks

Is this a case where you’re changing the User model after the project has been started?

If so, see Changing to a custom User model mid-project

If not, what has happened to put you in this situation? (I don’t see how this can happen in the normal routine.)

What I’m wondering is, if because of the situation you have found yourself in, is whether there are other hidden “gotchas” that you haven’t encountered yet as a result.

Hi Ken,

To be honest im not sure im doing the right thing, but i am building more APIs and these are being called from a React frontend.

I’ve been looking at different ways to do this and it look like using JWT with Djoser and DRF was a good approach.

I’ve created this class

class UserAccount(AbstractUser, PermissionsMixin):
    email = models.EmailField(unique=True)
    username = models.CharField(max_length=40, unique=True)
    first_name = models.CharField(max_length=40)
    last_name = models.CharField(max_length=40)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(auto_now_add=True)

    objects = UserAccountManager()
    groups = models.ManyToManyField(Group, blank=True, related_name='user_accounts')

    user_permissions = models.ManyToManyField(
        Permission,
        blank=True,
        related_name='user_accounts'
    )
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username', 'first_name', 'last_name']

    def get_full_name(self):
        return f"{self.first_name} {self.last_name}"

    def get_short_name(self):
        return self.first_name
    
    def __str__(self):
        return self.email

But when I run the migration I get the error about migration dependency and the django_admin_logs

Also, the app doesn’t contain data from the user side that I need to keep so didn’t seem like a big deal.

I’ve also added AUTH_USER_MODEL = 'accounts.UserAccount' because I’ve moved the authentication into its own app within the project. Is this a bad idea?

My previous question still applies - are you trying to create a new User model “mid-project”?

If so, the easiest thing to do is drop your database, delete all your migrations, run the initial migration, do makemigration, then migrate.

There are issues involved with switching User models mid-project that go beyond just the migration issue with the admin log model. See the docs referenced above.

Ok, sorry. I’m not necessarily looking to create a new user model. I want to add some additional fields and also set the login to email rather than username.

I’m assuming the default auth model would be fine to work with DFR and JWT, i just thought if it was straightforward it would be a cleaner way to go?

I understand that dropping the database would delete everything and then would need a way to restore it, but in the past using fixtures took forever to complete.

So i think i will need to see if i can modify the existing user model

Ignore all of this, i just switched back to using the original auth/usermodel and it’s looks fine.

Sorry, Ken.

Would you just clarify something for me, please?

If I stick with the existing user model which i think is auth_user and just add this:

from django.contrib.auth.models import AbstractUser, BaseUserManager, PermissionsMixin, Group, Permission

class UserAccount(AbstractUser, PermissionsMixin):
    email = models.EmailField(unique=True)
    username = models.CharField(max_length=40, unique=True)
    first_name = models.CharField(max_length=40)
    last_name = models.CharField(max_length=40)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(auto_now_add=True)

    objects = UserAccountManager()
    groups = models.ManyToManyField(Group, blank=True, related_name='user_accounts')

    user_permissions = models.ManyToManyField(
        Permission,
        blank=True,
        related_name='user_accounts'
    )
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username', 'first_name', 'last_name']

    def get_full_name(self):
        return f"{self.first_name} {self.last_name}"

    def get_short_name(self):
        return self.first_name
    
    def __str__(self):
        return self.email

Which allows me to set the email as the login etc. And then within my settings.py I add:

## AUTHENTICATION SETTINGS ##
AUTH_USER_MODEL = 'backend.UserAccount'

backend being my primary app within my project.

Is this going to be ok? Or whatever I do to make the login email_address will require me to do some manual data restores etc?

The structure of my project is:

app_backend
 -settings.py
backend
 -models.py
 -views.py
 -admin.py
...

So I’m just a bit confused if using

## AUTHENTICATION SETTINGS ##
AUTH_USER_MODEL = 'backend.UserAccount'

is this correct, or i should be using

## AUTHENTICATION SETTINGS ##
AUTH_USER_MODEL = app_backend.UserAccount'

and moving the model into app_backend and creating a new models.py?

Hope this makes sense.

When you do this:

You are no longer doing this:

Please read Substituting a custom User model and AUTH_USER_MODEL

Thanks, Ken.

I think I need to stick with the username as the authentication for logging in then. :frowning: