AttributeError: 'Test' object has no attribute '__name__'

78 Views Asked by At

This is the app.py. i faced this problem on my main project so i created a spearate test project to test it. and it occures again. here i tried to make a database in mongo then create a form out of it. pretty straight forward but no idea why the error happend

from flask_mongoengine.wtf import model_form
from flask_mongoengine import MongoEngine
from flask import render_template,request,Blueprint,Flask

app=Flask(__name__)
db= MongoEngine()

app.config\['MONGODB_SETTINGS'\]={'db':'project_database','host':'localhost','port':27017,'alias':'default'}
\#assign app to database
db.init_app(app)

class Test1(db.Document):
in1=db.StringField()
in2=db.StringField()
in3=db.StringField()

@app.route('/',methods=\['GET','POST'\])
def index():
test=model_form(Test1())
return render_template('inedx.html',test=test)

app.run(debug=True)

This is the error

Traceback (most recent call last):
  File "C:\Users\Give Up\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 2548, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\Give Up\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 2528, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\Give Up\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\Give Up\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\Give Up\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\Give Up\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "g:\Documents\onedrive\OneDrive - MSFT\Huzaifa\CODE\TESST\asdasdasdasd\app.py", line 25, in index
    test=model_form(Test1())
  File "C:\Users\Give Up\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_mongoengine\wtf\orm.py", line 306, in model_form
    return type(model.__name__ + "Form", (base_class,), field_dict)
AttributeError: 'Test1' object has no attribute '__name__'

I wanted to make a ModelForm from Mongo Document. But I have no idea what is the problem

1

There are 1 best solutions below

0
huzaifa saad On

Found the solution. pretty dumb that I didn't notice. but here is the right way

def index():
    test=model_form(Test1)
    test=test()
    return render_template('inedx.html',test=test)