0

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 ?

6
  • Could you post your models? Is worker somehow connected to the regular user model? Commented Mar 2, 2016 at 13:41
  • In fact, worker model contains only fields: ip (GenericIPAddress), created_date and last_updated_date. I tried to inherit from AbstractBaseModel but it contains a lot of unnecessary information like password etc. Commented Mar 2, 2016 at 13:52
  • I also tried to add foreign key to User in my Worker model, but then in admin panel User object must be created before creating Worker object. And Token object (from Django REST Framework) must also be created before creating worker :/ Commented Mar 2, 2016 at 13:54
  • What about inheriting from abstract base user? Or are you wanting to have this point to existing users? Commented Mar 2, 2016 at 14:00
  • Nope, I don't want to point to exisitng users. The problem is Token model from REST framework points to normal users. And if I add FK to User model in my Worker model it doesn't change anything because I still have to create normal User first, then Token and finally Worker. What I want is to have a separated Worker model and only worker objects can authenticate with token. Commented Mar 2, 2016 at 14:04

1 Answer 1

0

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()    
Sign up to request clarification or add additional context in comments.

4 Comments

Mmm... how about populating fields like password? It's in AbstractBaseUser class... I mean - I get this error: "You are trying to add a non-nullable field 'password' to bankworker without a default; we can't do that (the database needs something to populate existing rows)."
You could just define a default password like an api key if a user is never going to use it or be able to access it.
Do I have to create manager for my custom user to set password to some dummy value?
Well, when I want to create new Token I still need a normal Django User object. Is there any opportunity to force TokenAuthentication point to another user model?

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.