django best approach for creating multiple type users

后端 未结 2 842
温柔的废话
温柔的废话 2020-12-01 01:41

I want to create multiple users in django. I want to know which method will be the best..

class Teachers(models.Model):
    user = models.ForeignKey(User)
           


        
2条回答
  •  隐瞒了意图╮
    2020-12-01 02:20

    One approach I was following with Django 1.7 (works with 1.6 too) is to subclass AbstractUser

    from django.db import models
    from django.contrib.auth.models import AbstractUser
    
    class User(AbstractUser):
        balance = models.DecimalField(default=0.0, decimal_places=2, max_digits=5)
    

    To use your model you need to set it to be the one used for authentication in settings.py:

    AUTH_USER_MODEL = 'your_app.User'
    

    Also note that you will now have to use settings.AUTH_USER_MODEL when referencing your new User model in a relation in your models.

    from django.db import models
    from django.conf import settings
    
    class Transaction(models.Model):
        user = models.ForeignKey(settings.AUTH_USER_MODEL) # ForeignKey(User) will not work
    

提交回复
热议问题