我该如何解决这个错误?字段 'id' 需要一个数字但得到了 'aapl'
How can I fix this error? Field 'id' expected a number but got 'aapl'
我正在尝试创建一个股票博客页面,现在在这个项目中,我遇到了 url 的问题,因为我想做的是为每个 post 在每个股票名称中创建,我想在该页面中显示 post,我的解释可能有点混乱,但这是代码和错误,所以你可以看到。
错误
Traceback (most recent call last):
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\snin2\Desktop\basura\lapagina\app1\views.py", line 59, in StockView
stock_posts = Post.objects.filter(stock=sym.lower())
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\query.py", line 904, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\query.py", line 923, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1350, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1377, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1311, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1165, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\lookups.py", line 22, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\fields\related_lookups.py", line 115, in get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\fields\__init__.py", line 1774, in get_prep_value
raise e.__class__(
ValueError: Field 'id' expected a number but got 'aapl'.
urls.py(我只显示相关的)
from django.urls import path
from app1 import views
from .views import PostView, ArticleDetailView, AddPostView, UpdatePostView, DeletePostView, AddCategoryView, CategoryView, LikeView, MyPostsView, AddCommentView, UpdateCommentView, DeleteCommentView
app_name = 'app1'
urlpatterns = [
path('stock/<str:sym>/', views.StockView, name = 'stock'),
]
views.py
def StockView(request, sym):
stock_posts = Post.objects.filter(stock=sym.lower())
return render(request, 'app1/stockview.html', {'stock':stock_posts})
models.py
class StockNames(models.Model):
name = models.CharField(max_length=255)
symbol = models.CharField(max_length=255)
def __str__(self):
return self.symbol
class Post(models.Model):
title = models.CharField(max_length= 255)
header_image = models.ImageField(null = True, blank = True, upload_to = 'images/')
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextField(blank = True, null = True)
#body = models.TextField()
post_date = models.DateField(auto_now_add=True)
category = models.CharField(max_length=255, default='coding')
snippet = models.CharField(max_length=255)
likes = models.ManyToManyField(User, related_name = 'blog_posts')
stock = models.ForeignKey(StockNames, null=True, on_delete=models.CASCADE)
def total_likes(self):
return self.likes.count()
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('app1:article-detail', args=(self.id,))
要使用 symbol
过滤 Post
,您需要 跨越查找 作为,
stock_posts = Post.objects.filter(<b>stock__symbol=sym.lower()</b>)
我正在尝试创建一个股票博客页面,现在在这个项目中,我遇到了 url 的问题,因为我想做的是为每个 post 在每个股票名称中创建,我想在该页面中显示 post,我的解释可能有点混乱,但这是代码和错误,所以你可以看到。
错误
Traceback (most recent call last):
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\snin2\Desktop\basura\lapagina\app1\views.py", line 59, in StockView
stock_posts = Post.objects.filter(stock=sym.lower())
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\query.py", line 904, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\query.py", line 923, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1350, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1377, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1311, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1165, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\lookups.py", line 22, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\fields\related_lookups.py", line 115, in get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\fields\__init__.py", line 1774, in get_prep_value
raise e.__class__(
ValueError: Field 'id' expected a number but got 'aapl'.
urls.py(我只显示相关的)
from django.urls import path
from app1 import views
from .views import PostView, ArticleDetailView, AddPostView, UpdatePostView, DeletePostView, AddCategoryView, CategoryView, LikeView, MyPostsView, AddCommentView, UpdateCommentView, DeleteCommentView
app_name = 'app1'
urlpatterns = [
path('stock/<str:sym>/', views.StockView, name = 'stock'),
]
views.py
def StockView(request, sym):
stock_posts = Post.objects.filter(stock=sym.lower())
return render(request, 'app1/stockview.html', {'stock':stock_posts})
models.py
class StockNames(models.Model):
name = models.CharField(max_length=255)
symbol = models.CharField(max_length=255)
def __str__(self):
return self.symbol
class Post(models.Model):
title = models.CharField(max_length= 255)
header_image = models.ImageField(null = True, blank = True, upload_to = 'images/')
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextField(blank = True, null = True)
#body = models.TextField()
post_date = models.DateField(auto_now_add=True)
category = models.CharField(max_length=255, default='coding')
snippet = models.CharField(max_length=255)
likes = models.ManyToManyField(User, related_name = 'blog_posts')
stock = models.ForeignKey(StockNames, null=True, on_delete=models.CASCADE)
def total_likes(self):
return self.likes.count()
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('app1:article-detail', args=(self.id,))
要使用 symbol
过滤 Post
,您需要 跨越查找 作为,
stock_posts = Post.objects.filter(<b>stock__symbol=sym.lower()</b>)