Django REST Framework/Django

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

  • regular Django user
  • 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()    
    
    链接地址: http://www.djcxy.com/p/33804.html

    上一篇: Django rest框架JWT和自定义身份验证后端

    下一篇: Django REST框架/ Django