0

I am using built-in Django Admin, I have two types of users, need to call both using different urls i.e http://127.0.0.1:8000/admin/staff & http://127.0.0.1:8000/admin/customer need to break-down user model into two separate models Staff & Customer. Staff model will be used for Admin users & Customer model will be used for front-end users.

Note: I have kept db table names same as default auth_user, auth_group ...etc

I have created app named users in project

So far I have tried following things:

models.py

from django.contrib.auth.models import (
    AbstractUser, Permission, Group, GroupManager)
from django.db import models
from django.utils.translation import gettext_lazy as _

class Staff(AbstractUser):

    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL'
        verbose_name = _('staff')
        verbose_name_plural = _('staffs')
        db_table = 'auth_user'


class Customer(AbstractUser):

    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL'
        verbose_name = _('customer')
        verbose_name_plural = _('customers')
        db_table = 'auth_user'

admin.py

from django.contrib import admin
from django.contrib.auth.models import Group, User, Permission
from django.contrib.auth.admin import GroupAdmin, UserAdmin
from django.utils.translation import ugettext_lazy as _
from django import forms
from django.forms import ModelForm, Select
from users.models import Staff, Customer


class MyGroupAdminForm(forms.ModelForm):

    class Meta:
        model = Group
        fields = ('name', 'permissions')

    permissions = forms.ModelMultipleChoiceField(
        Permission.objects.exclude(content_type__app_label__in=[
                                   'auth', 'admin', 'sessions', 'users', 'contenttypes']),
        widget=admin.widgets.FilteredSelectMultiple(_('permissions'), False))


class MyUserAdminForm(forms.ModelForm):
    model = User
    groups = forms.ModelChoiceField(Group.objects, label='Role')

    class Meta:
        model = User
        fields = '__all__'


class CustomUserAdmin(UserAdmin):
    form = MyUserAdminForm
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups',)}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )


class CustomGroupAdmin(GroupAdmin):
    form = MyGroupAdminForm

# admin.site.unregister(User)
admin.site.unregister(Group)


# admin.site.register(User, CustomUserAdmin)
# admin.site.register(Group, CustomGroupAdmin)

admin.site.register(Staff, CustomUserAdmin)
admin.site.register(Group, CustomGroupAdmin)

settings.py

AUTH_USER_MODEL = "users.Staff"

Now when I am trying to change any staff user I am getting following error:

OperationalError at /admin/users/staff/1/change/
(1054, "Unknown column 'auth_user_groups.staff_id' in 'where clause'")
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/users/staff/1/change/
Django Version: 2.2.8
Exception Type: OperationalError
Exception Value:    
(1054, "Unknown column 'auth_user_groups.staff_id' in 'where clause'")
Exception Location: C:\Users\kalim.ullah\Envs\Cert\lib\site-packages\MySQLdb\connections.py in query, line 239
Python Executable:  C:\Users\kalim.ullah\Envs\Cert\Scripts\python.exe
Python Version: 3.8.0
Python Path:    
['D:\\projects\\certdashboard',
 'C:\\Users\\kalim.ullah\\Envs\\Cert\\Scripts\\python38.zip',
 'C:\\Users\\kalim.ullah\\Envs\\Cert\\DLLs',
 'C:\\Users\\kalim.ullah\\Envs\\Cert\\lib',
 'C:\\Users\\kalim.ullah\\Envs\\Cert\\Scripts',
 'c:\\users\\kalim.ullah\\appdata\\local\\programs\\python\\python38\\Lib',
 'c:\\users\\kalim.ullah\\appdata\\local\\programs\\python\\python38\\DLLs',
 'C:\\Users\\kalim.ullah\\Envs\\Cert',
 'C:\\Users\\kalim.ullah\\Envs\\Cert\\lib\\site-packages']
Server time:    Fri, 10 Jan 2020 13:00:41 +0000

Please guide me the proper way of doing this

Thanks

Kalim
  • 487
  • 6
  • 18
  • You can't have two models using the same `db_table`, the second one is overriding the first when you `makemigrations`. Look at your migrations, the table "auth_user" is for your `Customer` model. – dirkgroten Jan 10 '20 at 13:21
  • 1
    Your approach is wrong. Extend your user model with profiles (OneToOne), one for staff and one for customers. You can't have two AUTH_USER_MODELs – dirkgroten Jan 10 '20 at 13:22
  • I have user_type column in user table. I just want to show two different menu items in Admin 'Staff'& 'Customer' having different urls i.e 'admin/staff' & 'admin/customer'. Is it possible using single Model? – Kalim Jan 10 '20 at 13:41
  • 1
    Does this answer your question? [Multiple ModelAdmins/views for same model in Django admin](https://stackoverflow.com/questions/2223375/multiple-modeladmins-views-for-same-model-in-django-admin) – dirkgroten Jan 10 '20 at 13:46
  • Let me try this first – Kalim Jan 10 '20 at 14:59

0 Answers0