manch manch - 1 year ago 63
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
class object automatically created when you create a new user? If the
field is required, how do you create a user object and employee object at the same time?

Answer Source

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')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download