1

I 'm following a tutorial of how to use AbstractBaseUser model in Django project. Now I would like to go one step further by creating other models for example address_book and product.

When using defaulter user model, we put like this:

class User(models.Model):
    ....
class AddressBook(models.Model):
   ....
class Product(models.Model):
   ....

Now when I use like

class MyUser(AbstractBaseUser):

Which reference should I use in the AddressBook and Product class? (The user in the Address book is a foreign key from Class MyUser).

 class AddressBook(AbstractBaseUser) and class Product(AbstractBaseUser) or

 class AddressBook(models.Model) and class Product (models.model)?

Thanks for your help in advance!

1
  • 1
    Relationship fields can be used here. Commented Sep 25, 2022 at 18:38

1 Answer 1

1

In Python if you define a class like that

class ClassName(SuperClassName):
  ...

You are extending one or more existing classes. This is inheritance, not a reference.

If you want a reference you might want something like this:

class AddressBook(models.Model):
  user = models.ForeignKey('MyUser', on_delete=models.CASCADE)
  ...

For more detailed information I recommend looking at this page in the documentation.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi SvenTUM, thanks for your reply. Based on your answer, do I have to put class Meta: abstract=True? under the AddressBook class?
Hi @smoothy ! No, need for that. Django Models are pretty straight forward. You might want to get informed about Object Oriented Programming (OOP). That will include concepts like inheritance, abstract classes, etc.

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.