Ramiro Latigano
Ramiro Latigano

Reputation: 1

Problems with custom model user in Django 5.0

I'm pretty new in this world of django apps so I'm still learning how the things works.

I'm trying to do a CRUD for my work, so we can register some things like clients info, ours tasks, assistance... etc. In an enthusiastic and young learning spirit I decided to make a custom user model in order to be able to add a profile picture of the users of this app. I was part succeeded while I was able to add users and edit its info in the app but some problems came to me later:

  1. I can create users from Django admin panel but then this users can't logging neither the app nor the admin panel even when I created this with the superuser and staff marks on.

  2. I can make a relation between my users and groups of the model auth.Group.

To solve 1) or at least to live with it, I found I can create users with the terminal and the command 'createsuperuser' and remove permissions later although I suspect this 'removing permission' don't work at all. But for the purpose of this project I thought it wold be fine anyway at least for now.

I include this lines in my custom_user_model_apps' views.py of my django project:

...
from django.contrib.auth.models import AbstractUser
...

...
class User(AbstractUser):
    image = models.ImageField(upload_to=profile_picture_path, blank=True)
    birthday = models.DateField(null=True, blank=True)
    dark_mode = models.BooleanField(default=False)
    telephone = models.CharField(
        max_length=20, null=True, blank=True, default=None)
...

And in my project settings.py:

INSTALLED_APPS = [
...
my_app's_name.apps.AsistenciasConfig'
...
]

AUTH_USER_MODEL = 'my_app's_name.User'

I'm using this funciton to make the login part:

def Log_in_function(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)

        if user is not None:
            login(request, user)
            return redirect('home')
        else:
            messages.error(request, 'Wrong username or password.')

    form = AuthenticationForm()
    return render(request, 'core/login.html', {'form': form})

And I'm obtaining the 'Wrong username ...' message in my localhost and the admin panel.I'm gessing the 'authenticate' function is pointing some way to the users of 'auth.user' and not to my 'my_name_app.user' so when it tries to authenticate a giving username and pasword it fails because there is nothing there...

  1. Advancing with my project I thought it would be good filter some actions deppending on who is logged in, so I try to use django groups and this way I discovered that seems to be that the model Group that Django itself creates is completely separated of my custom user model 'User'. This way, even when I can create groups in django admin panel I can't assigning this to my users and all my user are part of all the groups I created (maybe because they where created with the command 'superuser') even when groups arrived much later than users.

I tried to add a 'group' field with both, 'ForeignKey' and 'ManyToMany' relations to 'auth.Group' from my user model but this just add a field in my user creation/editing form parallel to the Group of auth.Group and still can't assigning a particular group to a given user. So, to this point I'm pretty lost of what can I do to solve this issue.

Any idea?

Edit: I was finally able to do all I wanted thanks to @CoffeeBasedLifeform and his suggestion of make an extension of the django User model and removing all the 'links' to the custom model from the rest of my project and 'linking' the original (but extended) model of User instead.

PS: I'm also new in this community so I don't know how to close this thread as solved or indicate that in this case the CoffeBasedLifeform's answer was enough.

Upvotes: 0

Views: 74

Answers (0)

Related Questions