程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Django 对象过滤器预期 id (int) 但得到 str大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Django 对象过滤器预期 id (int) 但得到 str?

开发过程中遇到Django 对象过滤器预期 id (int) 但得到 str的问题如何解决?下面主要结合日常开发的经验,给出你关于Django 对象过滤器预期 id (int) 但得到 str的解决方法建议,希望对你解决Django 对象过滤器预期 id (int) 但得到 str有所启发或帮助;

我制作了 Django 博客项目,并且我已经实现了一个功能来按标签过滤帖子。可以选择某个标签并查看带有此特定标签的所有帖子。但是当我想通过这个标签过滤所有帖子时我遇到了麻烦。我收到一个错误,Django 需要 int number 而不是 str。如何解决这个问题?

这是我的代码

 models.py
 
class Tag(models.Model):
    name = models.CharFIEld(max_length=200,null=TruE)
    slug = models.SlugFIEld(max_length=250,unique=True,editable=falsE)
 
 
class ProjectPost(models.Model):
    tag = models.ForeignKey(tag,on_@R_489_9421@e=models.PROTECT)
 
 
 
url.py
 
urlpatterns = [
    ...
    path("projects/",vIEws.projects,name='projects'),path('projects/<slug:tag>/',name='projects_tag'),] 
 
 
vIEws.py
 
def projects(request,tag=NonE):
 
    if tag is not None:
        ProjectPost.objects.filter(tag=tag)
    else:
        posts = ProjectPost.objects.all()
 
    Tags = Tag.objects.all()
    context = {'posts':posts,'Tags':Tags}
    return render(request,'website/projects.HTML',context)

这是错误的

ValueError at /projects/romania/
FIEld 'ID' expected a number but got 'romania'.
request Method: GET
request URL:    http://127.0.0.1:8000/projects/romania/
Django Version: 3.2.5
Exception Type: ValueError
Exception Value:    
FIEld 'ID' expected a number but got 'romania'.
Exception LOCATIOn: /home/cristian/Desktop/GreatEcology my project/ecosite/venvecosite/lib/python3.8/site-packages/django/db/models/fIElds/__init__.py,line 1825,in get_prep_value
Python Executable:  /home/cristian/Desktop/GreatEcology my project/ecosite/venvecosite/bin/python
Python Version: 3.8.5
Python Path:    
['/home/cristian/Desktop/GreatEcology my project/ecosite/ecowebsite','/home/cristian/Anaconda3/lib/python38.zip','/home/cristian/Anaconda3/lib/python3.8','/home/cristian/Anaconda3/lib/python3.8/lib-dynload','/home/cristian/Desktop/GreatEcology my '
 'project/ecosite/venvecosite/lib/python3.8/site-packages']
Server time:    Wed,21 Jul 2021 14:36:51 +0000

解决方法

我猜您在 tag 变量中获得了标签名称,因此只需使用此名称进行过滤

def projects(request,tag=NonE):
    if tag:
        posts = ProjectPost.objects.filter(tag__name=tag)
    else:
        posts = ProjectPost.objects.all()
    ...
,

您可能正在使用 Model.objects.filter(id=tag) 之类的内容进行过滤,其中 tag 是一个现在包含 romania 的变量。可能错误位于行中的视图

ProjectPost.objects.filter(tag=tag) 

相反,只需通过 dunderscore 访问标签名称。

ProjectPost.objects.filter(tag__name=tag) 
,

在您的 views.py 中输入此内容

library(data.tablE)

x = data.table(col1 = rnorm(10),col2 = rnorm(10))

# adding a new column to y without modifying x

y = copy(X)
y$col3 = rnorm(10)

y
#>            col1       col2       col3
#>  1:  0.17515821  0.1176021 -0.1347739
#>  2:  2.20399094 -1.7367777 -1.1524410
#>  3: -1.40434275 -1.1361914 -1.8311661
#>  4: -0.49387179 -2.1259718 -1.3750332
#>  5:  1.46795492  0.3063904 -0.5809118
#>  6:  0.64698082  0.5736446 -0.5618565
#>  7:  1.29720045 -0.3974435  1.9326449
#>  8:  0.17654267  1.8410662 -0.3044560
#>  9:  0.03544842 -0.1519397  1.1844237
#> 10: -0.90924160 -1.9819158  1.5297478

大佬总结

以上是大佬教程为你收集整理的Django 对象过滤器预期 id (int) 但得到 str全部内容,希望文章能够帮你解决Django 对象过滤器预期 id (int) 但得到 str所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。