manch manch - 5 months ago 10
Python Question

Creating a new user after extending Django user model

Cant seem to find a response that clearly answers my question.

If you extend the Django user model with basic code like:

class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.CharField(max_length=100)


How does this affect creating a new user? Is
employee
class object automatically created when you create a new user? If the
department
field is required, how do you create a user object and employee object at the same time?

Answer

The employee object is not created automatically. To create a user object and an employee object at the same time, you can use the user instance as such

employee = Employee(user=user, depatment='xyz')
employee.save()
Comments