I’ve posted back a form and within my view I’d like to add a a field.
Editing the slug field. It is the slug field which I’ve decided to hide in the form and automatically generate it in the view. How can I append the slug field to the form?
if form.is_valid():
form.[want_to_add_slug_field_here] = slugify(form.cleaned_data['title'])
form.save()
I’m using (this is to hide the fields from the front end users completely as I want to automate these.
class LinkForm(forms.ModelForm):
class Meta:
model = Link
exclude = ('pub_date', 'slug', 'posted_by',)
So these fields are not on my form when it’s generating. I’m wanting to add these fields into the FORM before the save. Is this even possible?
There are many ways to deal with that (I assume you use ModelForm):
Use form’s
cleanmethod:Add the slug to the model before commiting:
Override your model’s save method:
Use a 3rd party autoslug field, for example django-autoslug
I personally use the 4th way.