尝试使用ForeignKey作者过滤用户帖子的QuerySet时出现以下错误。 Django有以下行的问题:
posts = Post.objects.filter(author=components[0])并吐出这个错误:
invalid literal for int() with base 10: 'arcetina'这是我的views.py :
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 :
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)I get the following error when trying to filter on a QuerySet of user posts, with a ForeignKey author. Django has problem with the following line:
posts = Post.objects.filter(author=components[0])And spews out this error:
invalid literal for int() with base 10: 'arcetina'Here is my views.py:
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)And here is my models.py:
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)最满意答案
正如错误所示,您正在传递一个字符串来过滤一个整数字段。 (实际上它是一个外键,但它存储为整数ID。)
如果要过滤相关模型中的字段,则需要使用双下划线语法:
posts = Post.objects.filter(author__username=components[0])另请注意,拥有全局posts是一个非常糟糕的主意,尤其是当您在视图中对其进行变更时。 所有请求都会看到相同的列表; 一旦您按用户对其进行过滤,或将其撤消,下一个请求将会看到已修改的查询集。 您应该删除全局变量并每次从头开始查询Posts模型。
As the error says, you're passing a string to filter a field that is an integer. (Actually it's a foreign key, but that is stored as an integer ID.)
If you want to filter on a field in the related model, you need to use the double-underscore syntax:
posts = Post.objects.filter(author__username=components[0])Note also that it's an extremely bad idea to have a global posts queryset, especially as you're mutating it in your view. All requests will see the same list; once you've filtered it by user, or reversed it, the next request will see the already-modified queryset. You should remove the global variable and query the Posts model from scratch each time.
更多推荐
发布评论