Django外键objects.get究竟该怎么写,djangoobjects.get,model定义如下:cl
Django外键objects.get究竟该怎么写,djangoobjects.get,model定义如下:cl
model定义如下:
class Tag(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=100,verbose_name='名称') def __unicode__(self): return self.nameclass Post(models.Model): id = models.AutoField(primary_key=True) author = models.ForeignKey(User) post_date = models.DateField(verbose_name='日期', auto_now=True) post_title = models.CharField(verbose_name='标题', max_length=100) post_content = RichTextField(verbose_name='内容') post_tag = models.ForeignKey(Tag, verbose_name='标签')
在views.py里我是这样写的:
def tag(request, tag): post_list = Post.objects.filter(post_tag.name = tag) return render_to_response('tag.html',{'post_list':post_list,'tag':tag})
直接报语法错误Unresolved reference ‘post_tag’,如果写成post_tag = tag,网页上会报
ValueError at /tag/test/invalid literal for int() with base 10: 'test'Request Method: GETRequest URL: http://127.0.0.1:8000/tag/test/Django Version: 1.7.1Exception Type: ValueErrorException Value: invalid literal for int() with base 10: 'test'
就是想筛选下post_tag等于tag的Post,究竟该怎么写?
post_tag 这里是一个对象
pythondef tag(request, tag): tag = Tag.object.get(name=tag) post_list = Post.objects.filter(post_tag=tag) return render_to_response('tag.html',{'post_list':post_list,'tag':tag})
django 的orm 查询属性不是用 . 而是用 __连接的,题主可以试试
post_list = Post.objects.filter(post_tag__name=tag)
编橙之家文章,
相关内容
- 爬虫状态码返回状态200,自己访问400,这是什么原因?,爬
- Python requests爬虫编码encoding error是什么问题,requestsen
- 适合Python应用的Vim缩进调试方法,pythonvim缩进调试,我的
- python list列表append方法的性能问题,pythonappend,作为客户
- Python有没有开源包处理GBK Unicode编码问题,pythonunicode
- 了解python flask.Response(generator())流内容处理的朋友请进,
- Ubuntu火狐浏览器可以用python脚本来控制吗?,ubuntupytho
- Python yield与斐波那契数列问题,pythonyield,def fib():
- Python函数无法运行源码有问题吗?,python源码,>>&g
- Python扩展包问调用C++扩展方式,python,对方只提供了.s
评论关闭