Py学习  »  MongoDB

如何在mongodb中运行继承类的方法?

T.Kul • 4 年前 • 337 次点击  

例如,我创建了从基类继承的ChildClass,如下所示:

class BaseClass

end

class ChildClass1 < BaseClass

end

或其他

class ChildClass2 < BaseClass

end

与基类一起工作时

但当你和一、二班的孩子一起工作时, 我不能先做childclass1.count或childclass1.first 错误如下:(保存为此处显示后使用子类时)

[2] pry(#<TodosController>)> @todo = "Todo::TodoM#{$xvars["user_id"].to_s}".constantize.new
=> #<Todo::TodoM5ca1157c51d2f56bc4d5e379 _id: 5ca45c5651d2f5988c2ef5fa, created_at: nil, updated_at: nil, user_id: nil, title: nil, completed: nil, due: nil, detail: nil, image: nil, member: nil, status: nil, _type: "Todo::TodoM5ca1157c51d2f56bc4d5e379">
[3] pry(#<TodosController>)> @todo = "Todo::TodoM#{$xvars["user_id"].to_s}".constantize.new(
[3] pry(#<TodosController>)*   title: $xvars["form_todo"]["title"],
[3] pry(#<TodosController>)* user_id: $xvars["user_id"])
=> #<Todo::TodoM5ca1157c51d2f56bc4d5e379 _id: 5ca45ca351d2f5988c2ef5fb, created_at: nil, updated_at: nil, user_id: BSON::ObjectId('5ca1157c51d2f56bc4d5e379'), title: "sdfsdf", completed: nil, due: nil, detail: nil, image: nil, member: nil, status: nil, _type: "Todo::TodoM5ca1157c51d2f56bc4d5e379">
[4] pry(#<TodosController>)> @todo.save!
MONGODB | localhost:27017 | todo2_development.insert | STARTED | {"insert"=>"todos", "ordered"=>true, "lsid"=>{"id"=><BSON::Binary:0x70237914801960 type=uuid data=0x0c897c8df79a4052...>}, "documents"=>[{"_id"=>BSON::ObjectId('5ca45ca351d2f5988c2ef5fb'), "_type"=>"Todo::TodoM5ca1157c51d2f56bc4d5e379", "title"=>"sdfs...
MONGODB | localhost:27017 | todo2_development.insert | SUCCEEDED | 0.006s
=> true
[5] pry(#<TodosController>)> @todo.first
NoMethodError: undefined method `first' for #<Todo::TodoM5ca1157c51d2f56bc4d5e379:0x00007fc31453c828>
from (pry):6:in `create'
[6] pry(#<TodosController>)> @todo
=> #<Todo::TodoM5ca1157c51d2f56bc4d5e379 _id: 5ca45ca351d2f5988c2ef5fb, created_at: 2019-04-03 07:11:48 UTC, updated_at: 2019-04-03 07:11:48 UTC, user_id: BSON::ObjectId('5ca1157c51d2f56bc4d5e379'), title: "sdfsdf", completed: nil, due: nil, detail: nil, image: nil, member: nil, status: nil, _type: "Todo::TodoM5ca1157c51d2f56bc4d5e379">
[7] pry(#<TodosController>)> @todo.save!
=> true
[8] pry(#<TodosController>)> @todo.count
NoMethodError: undefined method `count' for #<Todo::TodoM5ca1157c51d2f56bc4d5e379:0x00007fc31453c828>
from (pry):9:in `create'
[9] pry(#<TodosController>)> @todo.first
NoMethodError: undefined method `first' for #<Todo::TodoM5ca1157c51d2f56bc4d5e379:0x00007fc31453c828>
from (pry):10:in `create'
[10] pry(#<TodosController>)> @todo.first
NoMethodError: undefined method `first' for #<Todo::TodoM5ca1157c51d2f56bc4d5e379:0x00007fc31453c828>
from (pry):11:in `create'
[11] pry(#<TodosController>)> @todo.class
=> Todo::TodoM5ca1157c51d2f56bc4d5e379

我做错了什么? (是的,我从“创建”中看到错误,但它已保存!如图所示)

{
  "_id": ObjectId("5ca45ca351d2f5988c2ef5fb"),
  "_type": "Todo::TodoM5ca1157c51d2f56bc4d5e379",
  "title": "sdfsdf",
  "user_id": ObjectId("5ca1157c51d2f56bc4d5e379"),
  "updated_at": new Date(1554275508224),
  "created_at": new Date(1554275508224)
}
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54070
 
337 次点击  
文章 [ 1 ]  |  最新文章 4 年前
T.Kul
Reply   •   1 楼
T.Kul    5 年前

我发现儿童班的工作方式和基础班一样。一开始不起作用的原因是用户id为零。

enter image description here

而不是我期望的另一个收藏 enter image description here