I have a model with SlugField. Value of that field is created when the model instance is saved for the first time:
from django.db import models
from django.template.defaultfilters import slugify as default_slugify
class SlugModel(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100)
def save(self, *args, **kwargs):
if not self.pk:
self.slug = self.slugify(self.name)
return super(SlugModel, self).save(*args, **kwargs)
def slugify(self, tag):
slug = default_slugify(tag)
return slug
If i use that model in ModelForm the slug field is by default displayed.
from django.forms import ModelForm
class SlugModelForm(ModelForm):
class Meta:
model = SlugModel
How to automatically prevent all ModelForms from displaying of all of it’s SlugFields without manually specifying ModelForm.exclude or SlugField(editable=False) on each form/field?.
I think you could also extend Lychas response, create a base class and inherit from that one:
This is untested though.