Comments (14)
in models.py (outside your model class):
def get_populate_from(instance):
return '%s-%s' % (instance.home, instance.away, )
then in your model:
slug = AutoSlugField(populate_from=get_populate_from)
from django-autoslug.
@Allan-Nava yes, just pass to populate_from
a callable that returns the value you want to slug.
from django-autoslug.
max_length
from django-autoslug.
@fabiocaccamo ok perfect i added!
from django-autoslug.
in models.py (outside your model class):
def get_populate_from(instance): return '%s-%s' % (instance.home, instance.away, )then in your model:
slug = AutoSlugField(populate_from=get_populate_from)
Hi, FYI, today i just needed a more standardized solution, so I propose this solution for those who are interested:
from operator import attrgetter
def get_populate_from(instance):
attrs = [attr.replace("__", ".") for attr in instance.AUTOSLUG_FIELDS]
attrs_values = [attrgetter(attr)(instance) for attr in attrs]
return "-".join(attrs_values)
Just add an AUTOSLUG_FIELDS
constant with desired values:
class Example(models.Model):
AUTOSLUG_FIELDS = ("user__name", "title")
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=128)
slug = AutoSlugField(max_length=256, populate_from=get_populate_from, unique=True)
from django-autoslug.
Hi @fabiocaccamo ,
I have error with lambda:
Migrations for 'allejo':
bootcamp/allejo/migrations/0016_match_slug.py
- Add field slug to match
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/makemigrations.py", line 193, in handle
self.write_migration_files(changes)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/makemigrations.py", line 231, in write_migration_files
migration_string = writer.as_string()
File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/writer.py", line 163, in as_string
operation_string, operation_imports = OperationWriter(operation).serialize()
File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/writer.py", line 120, in serialize
_write(arg_name, arg_value)
File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/writer.py", line 84, in _write
arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/writer.py", line 293, in serialize
return serializer_factory(value).serialize()
File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/serializer.py", line 229, in serialize
return self.serialize_deconstructed(path, args, kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/serializer.py", line 101, in serialize_deconstructed
arg_string, arg_imports = serializer_factory(arg).serialize()
File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/serializer.py", line 164, in serialize
raise ValueError("Cannot serialize function: lambda")
ValueError: Cannot serialize function: lambda
I try with:
slug = AutoSlugField(populate_from='home, away')
from django-autoslug.
Hi @fabiocaccamo , i try this:
def get_fullname_match(self):
from slugify import slugify
#Cambiato formato della data in d m Y
d = self.game_date.strftime('%d %m %Y')
name = "%s %s %s %s" % (self.grade, self.home, self.away, d)
match = slugify(name, to_lower=True, separator="-")
return match
this is example, lol is the same XD
# autoslugify value from a dynamic attribute (i.e. a method)
slug = AutoSlugField(populate_from='get_full_name')
from django-autoslug.
In this case populate_from
must be a callable, and it will receive an instance
arg.
The callable must return a string, and AutoSlugField
will convert it to a slug.
Try as I told you.
from django-autoslug.
Now I try with populate_from
@fabiocaccamo
from django-autoslug.
@fabiocaccamo Is possibile extend the lenght of autoslug field?
from django-autoslug.
Please add this to documentation. The documentation still says lambda function can be used https://django-autoslug.readthedocs.io/en/latest/
from django-autoslug.
in models.py (outside your model class):
def get_populate_from(instance): return '%s-%s' % (instance.home, instance.away, )then in your model:
slug = AutoSlugField(populate_from=get_populate_from)Hi, FYI, today i just needed a more standardized solution, so I propose this solution for those who are interested:
from operator import attrgetter def get_populate_from(instance): attrs = [attr.replace("__", ".") for attr in instance.AUTOSLUG_FIELDS] attrs_values = [attrgetter(attr)(instance) for attr in attrs] return "-".join(attrs_values)Just add an
AUTOSLUG_FIELDS
constant with desired values:class Example(models.Model): AUTOSLUG_FIELDS = ("user__name", "title") user = models.ForeignKey(User, on_delete=models.CASCADE) title = models.CharField(max_length=128) slug = AutoSlugField(max_length=256, populate_from=get_populate_from, unique=True)
but why my slug that contains uppercase not work with this?
from django-autoslug.
@kmarilleau @kwhandy your idea is nice, but I think that it could be implemented better:
from operator import attrgetter
def get_populate_from(fields):
def wrapped(instance):
attrs = [attr.replace("__", ".") for attr in fields]
attrs_values = [attrgetter(attr)(instance) for attr in attrs]
return "-".join(attrs_values)
return wrapped
class Example(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=128)
slug = AutoSlugField(max_length=256, populate_from=get_populate_from("user__name", "title"), unique=True)
from django-autoslug.
Unfortunately, this method yields this error:
TypeError: get_populate_from() takes 1 positional argument but 2 were given
from django-autoslug.
Related Issues (20)
- ModuleNotFoundError: No module named 'django.core.urlresolvers' on python 3.6.3 HOT 7
- New release? HOT 12
- Support for Django 2.1.x HOT 11
- unique_with does not work with BooleanFields
- autoslug should work with no arguments
- Include LICENSE and run_tests.py in PyPI sdist
- autoslug 1.9.4 python 3.7.3 django 2.2 import successfull but importlib fails
- How to increase maximum length of AutoSlug field
- Feature request: generating slug from multiple fields HOT 2
- Slug is always regenerated HOT 3
- allow_unicode should not be popped off the kwargs HOT 2
- Uniqueness Manager is always overridden
- Doesn't work with Django 3.1 HOT 2
- error when used model.save() HOT 1
- Lambda functions no longer work with Django migrations
- slug that has a dash at the end initially is changed with next model save
- The letter "ษ,รง,ฤฑ" is not supported
- Unique atribute double sense.
- slug doesn't work peoperly in Farsi even with allow_unicode=True
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-autoslug.