Question

I am developing a project in python and used mongodb as database. I wanted to create a collection by using pymongo, here is what I have done:

First I created mongodb collection object by:

class ReportProblem(Collection):
collection = db.report_problem
report_problem = ReportProblem()

And in the python code what I did is :

def report_problem():    
   problem_data = dict(
      # Data which needs to be store
)
report_problem.new(**problem_data)

So I got an error:

AttributeError: 'function' object has no attribute 'new'" at line report_problem.new(**problem_data)"

Did anybody know why this is happened?

Was it helpful?

Solution

report_problem is a function, since you used the def keyword to define it. Functions don't have a method called .new().

If you defined your ReportProblem() instance to a variable named report_problem previously, then when you define the function report_problem, the instance is shadowed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top