2

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 Answer 1

1
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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.