python - Using Custom User DB Structure in Mysql for Django 1.8 -
i have question regarding table structure user
if extending functionalities, using mysql database.
given models.py file
class libraryuser(models.model): user_id = models.onetoonefield(user) is_catalogue_subscriber = models.integerfield(default=1) is_research_subscriber = models.integerfield(default=1) library_membership_number = models.charfield(max_length=64)
i have table structure sql
create table library_user( user_id int(10) primary key is_catalogue_subscriber integer(1) default 1 is_research_subscriber = integer(1) default 1 library_membership_number = varchar(16) )
so now, when fire server , access accounts in admin page, django throwing error:
exception type: operationalerror exception value: (1054, "unknown column 'library_user.id' in 'where clause'")
use
user = models.onetoonefield(user, primary_key=true)
i.e. drop _id in attribute name.
in case want define richer user model (i.e. add more attributes) can
use one-to-one relationship model containing fields additional information. one-to-one model called profile model, might store non-auth related information site user. example might create libraryuser model:
from django.contrib.auth.models import user class libraryuser(models.model): user = models.onetoonefield(user, on_delete=models.cascade) is_catalogue_subscriber = models.integerfield(default=1) is_research_subscriber = models.integerfield(default=1) library_membership_number = models.charfield(max_length=64)
assuming existing libraryuser fred smith has both user , libraryuser model, can access related information using django’s standard related model conventions:
>>> u = user.objects.get(username='fsmith') >>> freds_department = u.libraryuser.department
then add profile model’s fields user page in admin do
from django.contrib import admin django.contrib.auth.admin import useradmin django.contrib.auth.models import user my_user_profile_app.models import libraryuser # define inline admin descriptor employee model # acts bit singleton class libraryuserinline(admin.stackedinline): model = libraryuser can_delete = false verbose_name_plural = 'libraryuser' # define new user admin class useradmin(useradmin): inlines = (libraryuserinline, ) # re-register useradmin admin.site.unregister(user) admin.site.register(user, useradmin)
all taken official docs
Comments
Post a Comment