Code Monkey home page Code Monkey logo

django-sluggable's People

Contributors

thoas avatar viviencormier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

mysociety gilou

django-sluggable's Issues

Non-unique slugs?

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?

apps.get_model not initializing the manager

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!)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.