from django.contrib import admin

from .models import Email


class EmailAdmin(admin.ModelAdmin):
    exclude = ('targetted_users', 'is_sent')

admin.site.register(Email, EmailAdmin)

from django.contrib import admin

from .models import Email


class EmailAdmin(admin.ModelAdmin):
    readonly_fields = ('targetted_users', 'is_sent',)
    add_fieldsets = (
        (None, {
            'fields': ('subject', 'message', 'post'),
        }),
    )

    def get_fieldsets(self, request, obj=None):
        if not obj:
            return self.add_fieldsets
        return super(EmailAdmin, self).get_fieldsets(request, obj)

admin.site.register(Email, EmailAdmin)

Customize how fields on Email are displayed while adding & editing
Customize how fields on Email are displayed while adding & editing

- Hide fields `targetted_users`, `is_sent` while adding a new
  Email object
- Display all fields but make `targetted_users`, `is_sent` fields
  read only when editing an Email object

  from django.contrib import admin
  
  from .models import Email
  
  
  class EmailAdmin(admin.ModelAdmin):
-     exclude = ('targetted_users', 'is_sent')
+     readonly_fields = ('targetted_users', 'is_sent',)
+     add_fieldsets = (
+         (None, {
+             'fields': ('subject', 'message', 'post'),
+         }),
+     )
+ 
+     def get_fieldsets(self, request, obj=None):
+         if not obj:
+             return self.add_fieldsets
+         return super(EmailAdmin, self).get_fieldsets(request, obj)
  
  admin.site.register(Email, EmailAdmin)
  
Customize how fields on Email are displayed while adding & editing
## Code Before:
from django.contrib import admin

from .models import Email


class EmailAdmin(admin.ModelAdmin):
    exclude = ('targetted_users', 'is_sent')

admin.site.register(Email, EmailAdmin)

## Instruction:
Customize how fields on Email are displayed while adding & editing
## Code After:
from django.contrib import admin

from .models import Email


class EmailAdmin(admin.ModelAdmin):
    readonly_fields = ('targetted_users', 'is_sent',)
    add_fieldsets = (
        (None, {
            'fields': ('subject', 'message', 'post'),
        }),
    )

    def get_fieldsets(self, request, obj=None):
        if not obj:
            return self.add_fieldsets
        return super(EmailAdmin, self).get_fieldsets(request, obj)

admin.site.register(Email, EmailAdmin)
