Django: reference model fields from another model

Viewed 91

I have two models. What i need is to reference the name and the email field from the Users model to the Customer model fields. Is the following way correct?

class Users(AbstractBaseUser):
    name = models.CharField(max_length=200)
    email =  models.CharField(max_length=200)
from users.models import Users

class Customer(models.Model):
    user = models.OneToOneField(
        Users, on_delete=models.CASCADE, null=True, blank=True)

    name = models.OneToOneField(
        Users.name, on_delete=models.CASCADE, null=True, blank=True)

    email = models.OneToOneField(
        Users.email, on_delete=models.CASCADE, null=True, blank=True) 
       
1 Answers
class Users(AbstractBaseUser):
    name = models.CharField(max_length=200)
    email =  models.CharField(max_length=200)
    
class Customer(models.Model):
    user = models.OneToOneField(
        Users, on_delete=models.CASCADE, null=True, blank=True)

That's all. Then you can do for example

Customer.objects.get(pk=1).user.name
Related