Django IN query as a string result - invalid literal for int() with base 10

Posted by bmelton on Stack Overflow See other posts from Stack Overflow or by bmelton
Published on 2010-04-16T02:24:20Z Indexed on 2010/04/16 2:33 UTC
Read the original article Hit count: 398

Filed under:
|
|
|

Trying to query a 'Favorites' model to get a list of items a user has favorited, and then querying against a different model to get the objects back from that query to present to the template, but I'm getting an error: "invalid literal for int() with base 10"

Looking over all of the other instances of that error, I couldn't find any in which the asker actually wanted to work with a comma separated list of integers, so I'm kind of at a loss.

Model

class Favorite(models.Model):
    # key should be the model name, id is the model.id, and user is the User object.
    key     = models.CharField(max_length=255, unique=True)
    val     = models.IntegerField(default=0)
    user    = models.ForeignKey(User)

    class Admin:
            list_display = ('key', 'id', 'user')

View

def index(request):
    favorites = Favorite.objects.filter(key='blog', user=request.user.pk)
    values = ""

    for favorite in favorites:
            values += "%s," % favorite.val
    #values = "[%s]" % values

    blogs = Blog.objects.filter(pk__in=values)

    return render_to_response('favorite/index.html',
            {
                    "favorites"     : favorites,
                    "blogs"         : blogs,
                    "values"        : values,
            },
            context_instance=RequestContext(request)
    )

enter code here

© Stack Overflow or respective owner

Related posts about django

Related posts about int