简体   繁体   中英

How to use an existing table for user login in Django?

Since the tables already exist in the DB for login, I cant use the built-in Django models. I have used inspectdb to map the tables to the project which s working fine, but with login how do I use the existing user model for authentication the views part.

This is the User table:

class Credential(models.Model):
    username = models.CharField(max_length=255, unique=True)
    password = models.CharField(max_length=255)
    type = models.ForeignKey('Usertype', models.DO_NOTHING, db_column='type')  


    class Meta:
        managed = False
        db_table = 'tb_credential'
        verbose_name_plural = "Credentials"

    def __str__(self):
        return self.username

views.py

def login(request):

    form = LoginForm(request.POST)
    if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')

Since the tables already exist in the DB for login, I cant use the built-in Django models. I have used inspectdb to map the tables to the project which s working fine, but with login how do I use the existing user model for authentication the views part.

This is the User table:

class Credential(models.Model):
    username = models.CharField(max_length=255, unique=True)
    password = models.CharField(max_length=255)
    type = models.ForeignKey('Usertype', models.DO_NOTHING, db_column='type')  


    class Meta:
        managed = False
        db_table = 'tb_credential'
        verbose_name_plural = "Credentials"

    def __str__(self):
        return self.username

views.py

def login(request):

    form = LoginForm(request.POST)
    if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')

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