Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django model reference when using AbstractBaseUser model

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading