Добрый вечер,как заполнить поле slug автоматически через форму? Делаю доску объявлений,нужно чтобы ,когда человек заполнил все поля объявления, поле slug заполнилось автоматически от title,есть модель
class Advert(models.Model):
region = models.CharField(max_length=32)
city = models.CharField(max_length=30)
category = models.TextField()
title = models.CharField(max_length=32)
description = models.CharField(max_length=900)
photo = models.ImageField(upload_to='')
phone_number = models.CharField(max_length=11)
date = models.DateTimeField(auto_now_add=True, null=True)
slug = models.SlugField(unique=True, null=True, blank=True)
def __str__(self):
return self.title
есть форма
class AddAdvertForm(ModelForm):
region = forms.ModelChoiceField(queryset=Russian_Federation_Regions.objects.order_by('region'))
city = forms.Field(widget=forms.Select)
category = forms.ModelChoiceField(queryset=CategoryAdvert.objects.order_by('name'))
title = forms.CharField()
description = forms.CharField(widget=forms.Textarea)
phone_number = forms.CharField(widget=forms.NumberInput)
class Meta:
model = Advert
fields = '__all__'
и вюшка
class CreateAdvertView(View):
template_name = 'bulletinboard/add_advert.html'
form_class = AddAdvertForm
def get(self, request):
form = self.form_class()
return render(request, self.template_name, {'form': form})
def post(self, request):
form = self.form_class(request.POST, request.FILES)
if form.is_valid():
print(form.title)
form.save()
return HttpResponseRedirect('/advert/')
return render(request, self.template_name, {'form': form})