Django: Can't access model attributes

1.5k views Asked by At

Lately I started using my existing mysql database in Django. Implementing it worked

The database settings looks like this

DATABASES = {
'default': {
    'NAME': 'app_data',
    'ENGINE': 'django.db.backends.sqlite3',
},
'users': {
    'NAME': 'user_data',
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'test01',
    'USER': 'test01',
    'PASSWORD': 'test',
    'HOST': 'localhost',
    'PORT': '',
}

Using the 'users' database connection in my model 'MyUser' as followed.

class MyUser(models.Model):
    name = models.CharField(max_length=50)
    key = models.CharField(max_length=32)
    options = models.TextField(blank=True, null=True)
    description = models.TextField(blank=True, null=True)
    statistics = models.TextField(blank=True, null=True)
    create_time = models.DateTimeField()
    modify_time = models.DateTimeField()
    is_enabled = models.IntegerField(blank=True, null=True)
    password = models.CharField(max_length=50, blank=True, null=True)
    hosts = models.TextField(blank=True, null=True)
    remote_ips = models.CharField(max_length=200, blank=True, null=True)
    presets = models.TextField(blank=True, null=True)
    icons = models.TextField(blank=True, null=True)
    roles = models.TextField(blank=True, null=True)
    resources = models.TextField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'user'

Got the same output as above by inspecting the database with

python manage.py inspectdb --database=users

As I'm using an external database for my user authentication and my model is way different then the default so I wrote a custom backend using Django's default user model and copying users to it by their first login.

from django.conf import settings
from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User
from dashboard.models import MyUser

class UserAuthentication(object):

def authenticate(self, username=None, password=None):
    try:
        myuser = MyUser.objects.get(name=username);
    except MyUser.DoesNotExist:
        myuser = None
    if myuser and myuser.password == password:
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            user = User(username=username, password=password)
            user.save()
        return user
    return None

def get_user(self, user_id, user_name):
    try:
        return User.objects.get(pk=user_id)
    except User.DoesNotExist:
        return None

I'm not even able to confirm everything is working as I already got stuck when logging in.

Cannot resolve keyword 'name' into field. Choices are: id

I'm still a newbie in using Python and Django. Using Python 2.7.12 and Django 1.10.3 Appreciating any help, thanks for reading!

1

There are 1 answers

0
Özer On

You can manually select a Database to use. Check out these docs: https://docs.djangoproject.com/en/1.11/topics/db/multi-db/#manually-selecting-a-database-for-a-queryset

Basically, you can choose a DB when you query with MyUser.objects.using('users').all().

Does that solve your problem?