I have a field (slug) that is “required” in the model, but you want to change the field in the ModelAdmin class so that it is optional. If the user does not fill it out, it is automatically filled in with another field (name).
class SomeModel(model.Model):
name = model.CharField(max_length=255)
slug = model.SlugField(unique=True, max_length=255)
I tried to do this in various ways, for example, overriding get_form () in ModelAdmin or using the ModelForm class and specifically specify the form.
class SomeModelAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
form = super(self.__class__, self).get_form(request, obj, **kwargs)
form.slug.required = False
return form
However, not a single solution worked for me. Besides manually creating the form, is there an even faster solution?
I have many such forms, and doing it manually can be tedious and difficult to maintain.
source
share