首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django Python: int()的文本无效,基数为10:'arcetina‘

Django Python: int()的文本无效,基数为10:'arcetina‘
EN

Stack Overflow用户
提问于 2016-04-17 19:05:16
回答 1查看 290关注 0票数 0

在试图对用户帖子的QuerySet进行筛选时,使用ForeignKey作者进行筛选时,会出现以下错误。Django有以下行的问题:

代码语言:javascript
复制
posts = Post.objects.filter(author=components[0])

然后说出了这个错误:

代码语言:javascript
复制
invalid literal for int() with base 10: 'arcetina'

这是我的views.py

代码语言:javascript
复制
def post_list(request):
    global posts

    context = {'posts' : posts}

    for post in posts:
        if not post.field:
            post.field = 'Unspecified'

    if request.method == "POST":
        searchRegex = request.POST.get("searchregex")
        components = searchRegex.split()

        if searchRegex == "-reversedate":
            posts = posts.reverse()
            context = {'posts' : posts}

        if "-user" in searchRegex:
            posts = Post.objects.filter(author=components[0])

    return render(request, 'webapp/threadfeed.html', context)

这是我的models.py

代码语言:javascript
复制
class Post(models.Model):
    title = models.CharField(max_length=150)
    slug = models.SlugField()
    text = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    up_vote = 0 # num of up votes
    down_vote = 0 #num of down votes
    vote_total = up_vote - down_vote
    author = models.ForeignKey('auth.User', null=True, blank=True)

    CHOICES = [
        ('Hardware and OS', 'Hardware and OS'),
        ('Desktops', 'Desktops'),
        ('Tablets', 'Tablets'),
        ('Phones', 'Phones'),
        ('Wearables', 'Wearables'),
        ('Windows', 'Windows'),
        ('Mac OS X', 'Mac OS X'),
        ('Linux and Unix', 'Linux and Unix'),
        ('Programming and Computer Science', 'Programming and Computer Science'),
        ('Software Development', 'Software Development'),
        ('Web Development (Front)', 'Web Development (Front)'),
        ('Web Development (Back)', 'Web Development (Back)'),
        ('Mobile Development', 'Mobile Development'),
        ('Game Development', 'Game Development'),
        ('Algorithms and Data Structures', 'Algorithms and Data Structures'),
        ('Databases', 'Databases'),
        ('IDE / Text Editors', 'IDE / Text Editors'),
        ('Tutorial', 'Tutorial'),
        ('Opinion', 'Opinion'),
        ('Miscellaneous', 'Miscellaneous')
    ]
    field = models.CharField(choices=CHOICES, max_length=200, default='Unspecified')

    def __unicode__(self):
        return self.title.encode('utf-8')

    @models.permalink
    def get_absolute_url(self):
        return ('blog_post_detail', (), 
                {
                    'slug' :self.slug,
                })

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
        super(Post, self).save(*args, **kwargs)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-17 19:14:26

正如错误所述,您正在传递一个字符串来筛选一个整数字段。(实际上它是一个外键,但它作为一个整数ID存储。)

如果要对相关模型中的字段进行筛选,则需要使用双下划线语法:

代码语言:javascript
复制
posts = Post.objects.filter(author__username=components[0])

还请注意,拥有一个全局posts查询集是一个非常糟糕的主意,尤其是在您的视图中对其进行变异时。所有请求都将看到相同的列表;一旦用户对其进行筛选,或将其反转,下一个请求将看到已修改的queryset。每次都应该删除全局变量并从零开始查询Posts模型。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36680926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档