[Django]-Django – Slugify get lookup

6👍

You would need to put some restrictions on the value of the ‘name’ field only allowing [-A-Za-z]+, but you could do:

def my_request(request, name):
    un_slugified_name = name.replace('-', '')
    objects = MyModel.objects.get(name=unslugified_name)

However, the name you pass through the querystring would have to be exactly what’s in the database. YMMV. My advice, use a SlugField 🙂

2👍

Slugify is a Python function. To achieve your goal, Django would have to fetch the complete database (with at least the pk and the name field), calculate the slug for each row and compare this to the given parameter. And this would be very imperformant, so:

TLDR: No

Leave a comment