django-jet icon indicating copy to clipboard operation
django-jet copied to clipboard

get_changeform_initial_data not working

Open singlas opened this issue 6 years ago • 1 comments

I have a model in Django:

class Office(BaseModelWithNameAddressMobile):
    ...
    office_type = models.IntegerField(_("Office type"), choices=[(tag.value, _(tag.name)) for tag in OfficeType])
    ...

I am creating 5 different views in Admin Panel for this, one for each office_type. In the default admin panel, I was using the below code to set the value for office_type accordingly.

@admin.register(ZoneOffice)
class ZoneOfficeAdmin(OfficeAdmin):
    def get_changeform_initial_data(self, request):
        return {'office_type': OfficeType.ZONE.value}

    def get_queryset(self, request):
        return self.model.objects.filter(office_type=OfficeType.ZONE.value)

But, this stopped working when I moved my code to Django-jet.

Please take a look.

Thanks

singlas avatar Oct 09 '19 11:10 singlas

from django.contrib import admin
from .models import Office, ZoneOffice
from jet.admin import CompactInline

class CustomOfficeAdmin(admin.ModelAdmin):
    def get_changeform_initial_data(self, request):
        initial = super().get_changeform_initial_data(request)
        initial['office_type'] = OfficeType.ZONE.value
        return initial

@admin.register(Office)
class OfficeAdmin(CustomOfficeAdmin):
    pass

@admin.register(ZoneOffice)
class ZoneOfficeAdmin(CustomOfficeAdmin):
    def get_queryset(self, request):
        return self.model.objects.filter(office_type=OfficeType.ZONE.value)

ljluestc avatar Feb 11 '24 23:02 ljluestc