Jonathan
Jonathan

Reputation: 219

Django: Slugify Post Data

I'm trying to save some form data inputted by the user. I would like to slugify the "name" which was entered by the user, but dont want the slug field to show on the template that the user sees. I tried to do it manually with the sell function that you see below, but cant quite get it to work. I want to eventually save the slugified name into the Item model I have listed below. I'm sure there's a much smarter/simpler way than the sell function I'm currently using :P. Thanks in advance!

class Item(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=75)
    slug = models.SlugField(max_length=50, unique=True)
    is_active = models.BooleanField(default=True)
    image =  models.CharField(max_length=50)
    price = models.DecimalField(max_digits=9, decimal_places=2)
    quantity = models.IntegerField(default=1)
    description = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    shipping_price = models.DecimalField(decimal_places=2, max_digits=6)
    categories = models.ManyToManyField(Category)


class AddItem(forms.ModelForm):
    class Meta:
        model = Item
        exclude = ('user','slug','is_active',)

def sell(request):
        if request.method == "POST":
            form = AddItem(request.POST)
            item = form.save(commit=False)
            item.user = request.user
            item.is_active = True
            item.slug = slugify(form.name) **#not sure what this line should be?** 
            item.save()
            if form.is_valid():
                form.save()
                return HttpResponseRedirect('thanks.html')
            else:
                url = urlresolvers.reverse('register')
                return HttpResponseRedirect(url)

Upvotes: 0

Views: 509

Answers (2)

Zapix
Zapix

Reputation: 132

You can exclude slug from user form. And slugify in pre_save signal.

from django.dispatch import receiver
from django.db.models.signals import pre_save

@receiver(pre_save, sender=Item)
def iter_pre_save_handler(sender, instance, **kwargs):
    if not instance.pk:
        instance.slug = slugify(instance.name)

Upvotes: 3

XORcist
XORcist

Reputation: 4367

According to the docs, you can exclude a field from being rendered in a model form like this:

class PartialAuthorForm(ModelForm):
    class Meta:
        model = Author
        fields = ('name', 'title')

or

class PartialAuthorForm(ModelForm):
    class Meta:
        model = Author
        exclude = ('birth_date',)

or by setting editable=False on the Field instance in your model.

Once you have done this, you can override the save method of the model, as the comments in the OP have suggested:

#  shamelessly copied from http://stackoverflow.com/questions/837828/how-do-i-create-a-slug-in-django/837835#837835
from django.template.defaultfilters import slugify

class test(models.Model):
    q = models.CharField(max_length=30)
    s = models.SlugField()

    def save(self, *args, **kwargs):
        self.s = slugify(self.q)
        super(test, self).save(*args, **kwargs)

Upvotes: 0

Related Questions