views:

75

answers:

1

Am running into ValueError, here is the full traceback, the end of which is:

Exception Type: ValueError at /admin/blog/post/add/
Exception Value: invalid literal for int() with base 10: 'treef'

I'm using AutoSlugField from django-command-extensions.

I am trying to get django-tagging working, this is my models.py:

class Post(models.Model):
  """Blog post model."""
  title = models.CharField('title', max_length=120)
  slug = AutoSlugField('slug', unique=True, max_length=120, primary_key=True)
  body = models.TextField('body')
  published = models.DateTimeField('publish', default=datetime.now)
  category = models.ForeignKey(Category)
  tags = TagField()

  class Meta:
    verbose_name = ('post')
    verbose_name_plural = ('posts')
    ordering  = ('-published',)
    get_latest_by = 'published'

  def __unicode__(self):
    return self.title
A: 

I don't think this is causing your error, but you're missing populate_from in your AutoSlugField declaration, and I don't think you need unique=True:

class Post(models.Model):
"""Blog post model."""
title = models.CharField('title', max_length=120)
slug = AutoSlugField('slug', unique=True, max_length=120, primary_key=True)
    ... snip...

I think you want:

slug = AutoSlugField(populate_from = 'title', max_length=120, primary_key=True)

Or at least, that's how I use AutoSlugField in some of my code.

I think you're getting your error because django-tagging expects primary keys to be integers, whereas your primary key is going to be a string. Is there a good reason for your primary_key=True declaration? Why not just use the default of the automatically added id column?

If you're wanting to access your posts using the slug in your URLs (which after all is the point of slugs!), then given this entry in your urls.py:

url(r'post/(?P<slug>.+)/$', single_post)

You want a view like this:

def single_post(request, slug):
   post = get_object_or_404(Post, slug = slug)
   ...

Or you can use generic views.

Dominic Rodger
Yes, this is my URL pattern to reach a single post: url(r'post/(?P<slug>.+)/$', single_post), so I'd like it if if the slug is the primary key, is there no way to work around this?
Thank you for editing my question. I will remember to follow this easier to read format further :)
@user346816 - I've edited my answer to account for your comment. Does that help?
Dominic Rodger
I used another tagging 3rd party module called django-taggit and I asked on IRC, where they pointed out that in this file http://github.com/alex/django-taggit/blob/master/taggit/models.pyclass TaggedItem(TaggedItemBase): object_id = models.IntegerField()that is the culprit. Is there a way I can work around this? That is I can put in tags = TaggableManager() in my Post model and override the primary key to be taken as as a SlugField?
@user346816 - you really don't need to make `slug` a primary key - it really doesn't get you anything. There's already a `UNIQUE` index on it. Just use the `id` column as your primary key (the default behaviour) and you're done. I don't think you can do what you're currently doing without changing the internal of the tagging application, which seems like a bad idea, for little gain.
Dominic Rodger
Yes I saw through the code and modifying the module doesn't leave the application to be pluggable anymore. I changed my Post's model to let Django take care of the Primary key field (default id), it all works great now :) Thank you!
@user346816 - excellent - if you get a moment, can you mark this answer as accepted by clicking the tick next to it?
Dominic Rodger