简体   繁体   中英

Django REST Framework/Django - extra user model

In my project I've two different types of users:

  1. regular Django user
  2. worker - user which sends messages through application API and authenticates with token (basic Django rest framework token authentication)

The problem is that Token object is connected with django User object. What would be the best solution - create new user model for my worker? I don't want to extend User model because I want to have "Users" and "Workers" in my admin panel.

class Worker(models.Model):
    ip = models.GenericIPAddressField()
    created_date = models.DateTimeField()
    last_update = models.DateTimeField()

    #  but it's not the way I wish it was done:
    #  user = models.ForeignKey(User)

In general how would you solve the problem when part of users login normally with login/password and some of the users (workers) use token as an authenticating credentials ?

Assuming you are wanting to have those same fields but want the table space to be unique you could do:

class Worker(models.AbstractBaseUser):
    ip = models.GenericIPAddressField()
    created_date = models.DateTimeField()
    last_update = models.DateTimeField()

Otherwise, if you want the worker to inherit you could do what is essentially a join by straight inheriting your auth user class by doing:

class Worker(AUTH_USER_MODEL):
    ip = models.GenericIPAddressField()
    created_date = models.DateTimeField()
    last_update = models.DateTimeField()    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM