0

I have a use case here and I don't know how to go about it. I have Custom User and this works well when adding a new user. But then I have another model which I would like the Custom User to populate. It is kind of link a main form subform approach but no data on the main form is going to be edited but only creating new records on the formset (subform).

from django.core.validators import RegexValidator
from sos.models import Company 
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.validators import MinValueValidator, MaxValueValidator
from django.urls import reverse  # page 138 Django for Beginneers 

# Create your models here.

#  https://stackoverflow.com/questions/19130942/whats-the-best-way-to-store-phone-number-in-django-models/19131360

class CustomUser(AbstractUser):
    position = models.CharField(max_length=30, null=True )   
    email = models.EmailField(null=True, blank=False, unique=True )
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
    mobile_phone = models.CharField(validators=[phone_regex], max_length=17, blank=False, null=True, unique=True)
    date_created = models.DateTimeField(auto_now_add=True, editable=True)
    is_staff = models.BooleanField(verbose_name="Is Staff Admin?", help_text="Designates whether this user should be able to log in the Admin Interface.")
    company = models.ForeignKey(Company , on_delete=models.CASCADE, verbose_name="Company Name") 
    
    def __str__(self):
        return self.username

    def get_absolute_url(self):
        return reverse("customuser_detail", args=[str(self.id)])

    class Meta:
        ordering = ["username"] 
         # email_address = models.EmailField(null=True, blank=False, unique=True )


class UserTargets(models.Model):

    SAFETY_CATEGORY = (
        ('SOS', 'Safety Observation'),
        ('Leadership_Walk', 'Leadership Walk'),
       )

    custom_user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    safety_category =  models.CharField(max_length=30, choices=SAFETY_CATEGORY, verbose_name='Safety Category')   
    targets = models.IntegerField(default=1, validators=[MinValueValidator(1),
                                       MaxValueValidator(100)],verbose_name='Targets to Achieve')  
                                       #  https://stackoverflow.com/questions/849142/how-to-limit-the-maximum-value-of-a-numeric-field-in-a-django-model
    date_created = models.DateTimeField(auto_now_add=True, editable=True)  # setting editable equal=False wouldnot make this contorl show on the form
    date_updated = models.DateTimeField(auto_now=True, editable=True)

    def __str__(self):
        return self.safety_category

    class Meta:
        ordering = ["date_created"]
        verbose_name_plural = "User_Targets"

Can someone please provide some guidance as I don't know what to do? Help...!

4

0 回答 0