thoas / django-sluggable Goto Github PK
View Code? Open in Web Editor NEWManage your slugs and redirect old slugs to the new one
Home Page: django-sluggable.rtfd.org
Manage your slugs and redirect old slugs to the new one
Home Page: django-sluggable.rtfd.org
Hi,
I have a scenario where I do id-slug as the actual slug, therefore, I don't need the slug to be unique, but it seems that wasn't planned ahead. What would you suggest as a path to handle that?
My idea would be: simply make is_slug_available aware of the uniqueness, that would be set either in the SluggableField instance, or in the decider field?
SluggableField(decider=MySlug, unique=False, populate_from='title')
Or in the decider slug field:
class MySlug(Slug): slug = models.CharField(db_index=True, unique=False)
What do you think?
Hi,
This might not be directly caused by sluggable, but it occurs and I wanted your opinion. Thanks a lot btw for that simple yet very functional app.
sluggable version is 0.7.0, installed from pip, python is 3.8, and Django 2.2
So here goes, I do have a model called Workshop, that has a slug field, defined as:
SluggableField(decider=WorkshopSlug, null=False, populate_from='title', max_length=150, always_update=True)
And WorkshopSlug looks like:
class WorkshopSlug(Slug):
slug = models.CharField(
max_length=255, verbose_name=_("URL"), db_index=True
)
class Meta:
abstract = False
This seems rather basic, my idea is to roll out a migration to populate that slug, on an existing db. The title is not allowed to be blank, and none are NULL.
I don't need uniqueness on the slug as I use pk-slug, but I tried without redefining WorkshopSlug.slug, I have the same issue.
I then tried a few things, and in the shell did something like:
for w in Workshop.objects.filter(slug__isnull=True): w.save()
And that worked nicely, the slugs were defined, I was happy. I rolled back the migrations, replayed the one creating the field and model.
Then, I wrote a migration, supposed to run after the one introducing the slug field, and the WorkshopSlug model in the db. Here's the migration:
from django.db import migrations
def set_initial_slugs(apps, schema_editor):
Workshop = apps.get_model('workshops', 'Workshop')
db_alias = schema_editor.connection.alias
for w in Workshop.objects.using(db_alias).all():
w.save(using=db_alias)
print('Workshop', w.title, 'has now slug', w.slug)
class Migration(migrations.Migration):
dependencies = [
('workshops', '0019_workshop_slugs'),
]
operations = [
migrations.RunPython(set_initial_slugs, migrations.RunPython.noop),
]
And that doesn't work for some reason, because the Slug is not instanciated (i.e. no signals, no self.decider and what not.). If I try something like w.slug = 'blah', it crashes as it can't get self.decider.
So easy workaround is to directly import the Workshop model, and that works for me. But you're not supposed to do that in a migration, and get them from get_model... However, is that a bug? Is it expected (took me a good hour to understand why it worked in the shell and not in my migration, because in the shell.... apps.get_model ... works!)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.