1

I am trying to implement import and export in Django admin.i am getting below error.

    admin.site.register(ShiftChange,ShiftChangeAdmin)
  File "/Users/shaileshyadaav/PycharmProjects/first/venv/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 117, in register
    raise AlreadyRegistered(msg)
django.contrib.admin.sites.AlreadyRegistered: The model ShiftChange is already registered with 'apple.ShiftChangeAdmin'.

I have referred(https://stackoverflow.com/a/13709239)but don't know how to unregister.Please find the below admin.py file.

  from django.contrib import admin
    from apple.models import ShiftChange,RMSI
    from import_export.admin import ImportExportModelAdmin
    # Register your models here.
    
    
    @admin.register(ShiftChange)
    class ShiftChangeAdmin(ImportExportModelAdmin):
        pass

    @admin.register(RMSI)
    class RMSIAdmin(ImportExportModelAdmin):
        pass
 
    
    class ShiftChangeAdmin(admin.ModelAdmin):
        list_display=['ldap_id','Vendor_Company','EmailID','Shift_timing','Reason','last_updated_time']
        ###Adding this line so that we Can Search/filter user result in case of any changes or to Check last time when he updated#######
        search_fields = ('ldap_id', 'EmailID','Shift_timing')
    
    admin.site.register(ShiftChange,ShiftChangeAdmin)
    
    class RMSIAdmin(admin.ModelAdmin):
        list_display=['ldap_id','Vendor_Company','EmailID','Shift_timing','Reason','last_updated_time']
        ###Adding this line so that we Can Search/filter user result in case of any changes or to Check last time when he updated#######
        search_fields = ('ldap_id', 'EmailID','Shift_timing')
    
    admin.site.register(RMSI,RMSIAdmin)

Any help on this will be highly appreciated.

Shailesh Yadav
  • 301
  • 2
  • 16

1 Answers1

2

to register a model and make it available for editing via Admin interface you have two options:

  • using register function with admin.site.register(YOURMODEL, YOURMODELAdmin)
  • using register decorator via @admin.register(YOURMODEL)

refer to this doc https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#modeladmin-objects for more details

in your case your mixing the two approaches and you have to choose one, that's why it raises the error

register raise AlreadyRegistered(msg)

and below how i managed to solve it.

refer to this doc https://django-import-export.readthedocs.io/en/latest/getting_started.html#admin-integration

you don't need to extend/subclass admin.ModelAdmin since ImportExportModelAdmin already extends it.

from django.contrib import admin
from apple.models import ShiftChange,RMSI
from import_export.admin import ImportExportModelAdmin

# Register your models here.

# @admin.register(ShiftChange)
# class ShiftChangeAdmin(ImportExportModelAdmin):
    # pass

# @admin.register(RMSI)
# class RMSIAdmin(ImportExportModelAdmin):
    # pass
    

class ShiftChangeAdmin(ImportExportModelAdmin):  # HERE
    list_display=['ldap_id','Vendor_Company','EmailID','Shift_timing','Reason','last_updated_time']
    ###Adding this line so that we Can Search/filter user result in case of any changes or to Check last time when he updated#######
    search_fields = ('ldap_id', 'EmailID','Shift_timing')
    
admin.site.register(ShiftChange,ShiftChangeAdmin)
    
class RMSIAdmin(ImportExportModelAdmin):  # HERE
    list_display=['ldap_id','Vendor_Company','EmailID','Shift_timing','Reason','last_updated_time']
    ###Adding this line so that we Can Search/filter user result in case of any changes or to Check last time when he updated#######
    search_fields = ('ldap_id', 'EmailID','Shift_timing')

admin.site.register(RMSI,RMSIAdmin)
cizario
  • 3,995
  • 3
  • 13
  • 27
  • Hi Cizario Thanks!, I am getting below error File "/Users/shaileshyadaav/PycharmProjects/first/venv/lib/python3.7/site-packages/django/forms/widgets.py", line 186, in __new__ new_class = super(MediaDefiningClass, mcs).__new__(mcs, name, bases, attrs) TypeError: Cannot create a consistent method resolution order (MRO) for bases ModelAdmin, ImportExportModelAdmin I think it is not able to decide which option to choose first (C3 or MRO.) – Shailesh Yadav Sep 28 '20 at 15:03
  • @ShaileshYadav i have no clue about this error but i'm reading the doc of `django-import-export` package – cizario Sep 28 '20 at 15:08
  • @ShaileshYadav i updated my answer: your don't need to extend `admin.ModelAdmin` since `ImportExportModelAdmin` already extends `admin.ModelAdmin`, let me know if this solves your issue – cizario Sep 28 '20 at 15:21
  • Yes it working and issue is resolved.Thanks!.Can you please share me document link which you referred. – Shailesh Yadav Sep 28 '20 at 15:33
  • i already updated my answer with the link : https://django-import-export.readthedocs.io/en/latest/getting_started.html#admin-integration – cizario Sep 28 '20 at 15:34