Hello!
I've created a Django app for my company to track employee infractions (missed punch, late to a shift, etc.), but I'm at a roadblock in terms of adding users. Currently, the only way I can add users is through the command line. I'd much rather be able to add users through the django.contrib.admin site. However, I've found that if I input a password there, it treats is as the value to enter into the database (post hashing/salting/etc.) rather than raw text. This obviously does not work.
I'm using an AbstractUser model called Employee to represent users instead of the default User model. This is just so I have the option of customizing my model down the road if need be without having to deal with broken ForeignKey relationships.
If this were on the frontend, I would write a view that would take the raw password and send it through set_password(), and that would be the end of it. But I'm not sure how to do that with the admin site.
Models.py:
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class Employee(AbstractUser):
def __str__(self):
return self.first_name
class InfractionType(models.Model):
description = models.CharField(max_length=30)
def __str__(self):
return self.description
class Infraction(models.Model):
timestamp = models.DateTimeField()
employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
type = models.ForeignKey(InfractionType, on_delete=models.CASCADE)
has_comment = models.BooleanField(default=False)
description = models.CharField(max_length=200)
def __str__(self):
return str(self.employee) + ": " + self.description
Admin.py:
from django.contrib import admin
# Register your models here.
from .models import Employee, InfractionType, Infraction
class InfractionInline(admin.TabularInline):
model = Infraction
extra = 0
class EmployeeAdmin(admin.ModelAdmin):
fieldsets = [
('Authentication and Metadata', {'fields': ['username', 'first_name', 'last_name', 'email', 'groups', 'is_staff', 'is_active'], 'classes': ['collapse']}),
]
inlines = [InfractionInline]
# Prevent users from getting deleted (should be made inactive instead)
def has_delete_permission(self, request, obj=None):
return False
admin.site.register(Employee, EmployeeAdmin)
admin.site.register(InfractionType)
What I see on the admin site:
Thanks for your help!