Hi guys,
I have this issue I can not work out for the life of me. I would really
appreciate it if someone could help.
This is my model:
class Business(models.Model):
...
slug = models.SlugField("Slug", max_length=255, unique=True,
blank=False)
I have the following function in the models.py file that generates the
unique slug.
def unique_slug(item,slug_source,slug_field):
if getattr(item, slug_field): # if it's already got a slug, do nothing.
slug = slugify(getattr(item,slug_source)) #get the value of slug_source
from the item and slugify it
itemModel = item.__class__
# the following gets all existing slug values
allSlugs = [sl.values()[0] for sl in itemModel.objects.values(slug_field)]
if slug in allSlugs:
counterFinder = re.compile(r'-\d+$')
counter = 2
slug = "%s-%i" % (slug, counter)
while slug in allSlugs:
slug = re.sub(counterFinder,"-%i" % counter, slug)
counter += 1
setattr(item,slug_field,slug)
I have overridden the save method for Business class with this:
def save(self, *args, **kwargs):
self.slug = unique_slug(self, slug_source='slug', slug_field='slug')
super(Business, self).save(*args, **kwargs)
The issue I have is that this will work in shell:
b = Business(name="some name", slug="some slug")
slug = unique_slug(b, slug_source='slug', slug_field='slug')
super(Business, b).save()
But this will not work:
b = Business.objects.create(name="some name", slug="some slug")
The error I get is:
"IntegrityError: bookings_business.slug may not be NULL"
I don't get it what the difference is and what I'm missing here. Any help
is highly appreciated guys. Thanks a million!
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.