Home > Net >  Django model reference when using AbstractBaseUser model
Django model reference when using AbstractBaseUser model

Time:09-26

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!

CodePudding user response:

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.

  • Related