justinmayer / django-autoslug

AutoSlugField for Django. Supports (but not does not require) unidecode/pytils for transliteration. Old issue tracker is at Bitbucket: https://bitbucket.org/neithere/django-autoslug/issues
https://readthedocs.org/projects/django-autoslug/
GNU Lesser General Public License v3.0
295 stars 105 forks source link

error when used model.save() #69

Open i-m-d opened 4 years ago

i-m-d commented 4 years ago

Hello,

i have the following:

from django.db import models
from autoslug import AutoSlugField
from autoslug.settings import slugify as default_slugify

class Cimas(models.Model):

  def slugify_sierra(self, value):
    return default_slugify(value.replace('/', '-'))

  c_nombre = models.CharField(
    max_length = 150,
  )
  c_sierra = models.CharField(
    max_length = 150,
    blank = True,
    null = True,
  )
  sierra_slug = AutoSlugField(
    max_length = 150,
    populate_from = 'c_sierra',
    slugify=slugify_sierra,
  )
  c_estado = models.CharField(
    choices = ESTADOS,
    max_length = 11,
    default = 'Desconocida',
  )

  --- more fields ---

When i edit a record slug filed is change correctly without error, but when a use Model.save() in shell or in a funcition i've got this error:

python3 manage.py shell
Python 3.6.3 (v3.6.3:2c5fed86e0, Oct  3 2017, 00:32:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from senderismo.models import Cimas

In [2]: cima = Cimas.objects.filter(c_nombre = "Zaria")[0]

In [3]: cima
Out[3]: <Cimas:   768 - Zaria (625 m) - estado: Pendiente - buzón: Con buzón>

In [4]: cima.c_sierra
Out[4]: 'Igorin/bianditz'

In [5]: cima.sierra_slug
Out[5]: 'igorin-bianditz'

In [6]: cima.c_estado
Out[6]: 'Pendiente'

In [7]: cima.c_estado = "Hecha"

In [8]: cima.c_estado
Out[8]: 'Hecha'

In [9]: cima.save()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-ef79aeddcd43> in <module>
----> 1 cima.save()

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py in save(self, force_insert, force_update, using, update_fields)
    739
    740         self.save_base(using=using, force_insert=force_insert,
--> 741                        force_update=force_update, update_fields=update_fields)
    742     save.alters_data = True
    743

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py in save_base(self, raw, force_insert, force_update, using, update_fields)
    777             updated = self._save_table(
    778                 raw, cls, force_insert or parent_inserted,
--> 779                 force_update, using, update_fields,
    780             )
    781         # Store the database on which the object was saved

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py in _save_table(self, raw, cls, force_insert, force_update, using, update_fields)
    846             base_qs = cls._base_manager.using(using)
    847             values = [(f, None, (getattr(self, f.attname) if raw else f.pre_save(self, False)))
--> 848                       for f in non_pks]
    849             forced_update = update_fields or force_update
    850             updated = self._do_update(base_qs, using, pk_val, values, update_fields,

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py in <listcomp>(.0)
    846             base_qs = cls._base_manager.using(using)
    847             values = [(f, None, (getattr(self, f.attname) if raw else f.pre_save(self, False)))
--> 848                       for f in non_pks]
    849             forced_update = update_fields or force_update
    850             updated = self._do_update(base_qs, using, pk_val, values, update_fields,

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/autoslug/fields.py in pre_save(self, instance, add)
    274         slug = None
    275         if value:
--> 276             slug = self.slugify(value)
    277         if not slug:
    278             slug = None

TypeError: slugify_sierra() missing 1 required positional argument: 'value'

What am i doing wrong ?

What i want to do is change some Cimas fileds (not slug filed) from a function

Verison: django-autoslug 1.9.8

NOTE: sorry for my bad English.

Thank you.

mathemaat commented 4 years ago

Move your function outside of the class, and remove the self parameter:

def slugify_sierra(value):
    return default_slugify(value.replace('/', '-'))

class Cimas(models.Model):
  # more code here
  sierra_slug = AutoSlugField(
    max_length = 150,
    populate_from = 'c_sierra',
    slugify=slugify_sierra,
  )
  # more code here