0

I am new to django and am trying to set up a simple employee timesheet site that has multiple users. I set up two models one for the individual employee that has a ForeignKey of the base django user and a timesheet model that has a ForeignKey of the employee model. I'm not sure this is correct because when I use my registration form it just creates the base django user and not the "Employee" so when I want to create a new timesheet entry only the one employee is set up (set up with admin page). Can someone with more django experience tell me if there is a better way to do this (different model relationship, etc)

from django.urls import reverse
from django.core.validators import MinValueValidator, MaxValueValidator
from django.utils import timezone
import datetime
from django.contrib.auth.models import User

class Employee(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='employee')
    payRate = models.DecimalField(max_digits=4, decimal_places=2, default=15.00, verbose_name=("Pay"))
    vacTotal = models.DecimalField(max_digits=5, decimal_places=2, default=200.00, verbose_name=("Vacation"))

# META CLASS
    class Meta:
        verbose_name = 'employee'
        verbose_name_plural = 'employees'  

    # TO STRING METHOD
    def __str__(self):
        return f"{self.user}"



class Tsheet(models.Model):   

    # CHOICES
    WORK_CHOICES= (
    ('W', 'Regular Work'),
    ('V', 'Vacation'),
    ('S', 'Sick',),
    ('C','Call In'),
)

    # DATABASE FIELDS   
    name = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='name')
    workType = models.CharField(max_length=15,choices=WORK_CHOICES)
    workDate = models.DateField(verbose_name=("Date"), default=datetime.date.today, editable=True)
    workDescription = models.CharField(max_length=200)
    workHours = models.DecimalField(max_digits=4, decimal_places=2, default=8.00, verbose_name=("Hours"))
    workReviewed= models.BooleanField(default=False)
    slug = models.SlugField(max_length=50, unique=True,
                                    help_text='Unique value for timesheet entry URL, created automatically from name.')


     # META CLASS
    class Meta:
        verbose_name = 'tsheet'
        verbose_name_plural = 'tsheets'

    # TO STRING METHOD
    def __str__(self):
        return f"{self.name} - {self.workDate} - {self.workHours} - {self.workType}"

    # SAVE METHOD    

    # ABSOLUTE URL METHOD
    def get_absolute_url(self):
        return reverse('entry-detail', kwargs={'pk': self.pk})```
RGolf77
  • 42
  • 5
  • If you can accept the dirty way you can just attach a create signal handler to create employee object when user is created. Otherwise you will need to override the default form. – glee8e May 01 '19 at 23:10

1 Answers1

0

The right way to approach this is to extend the AbstractUser and add the fields there:

class User(AbstractUser):
    payRate = models.DecimalField(max_digits=4, decimal_places=2, default=15.00, verbose_name=("Pay"))
    vacTotal = models.DecimalField(max_digits=5, decimal_places=2, default=200.00, verbose_name=("Vacation"))

Then you have a single table with all the data from the default Django User as well as your specific fields

Daniele Bernardini
  • 1,516
  • 1
  • 12
  • 29
  • This worked, thanks. When I use the built in Admin page to set up a user now the fields are jumbled up, how can I customize this? – RGolf77 May 09 '19 at 23:47
  • Check this answer, it provides an example of how to extend the admin form https://stackoverflow.com/questions/23697130/django-admin-form-for-creating-abstractuser-extended-model – Daniele Bernardini May 10 '19 at 03:10