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 entuthiastic 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 succeded while I was able to add users and edit its info in the app but some problems came to me later:
-
I can create users from Django admin pannell but then this users can’t logging neither the app nor the admin pannell even when I created this with the superuser and staff marks on.
-
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 altought I suspect this ‘removing permission’ don’t work at all. But for the purpose of this project I tought 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…
- 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, ‘ForeingKey’ and ‘ManyToMany’ relations to ‘auth.Group’ from my user model but this just add a field in my user creation/editing form parrallel 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?
Ramiro Latigano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.