0

Basically I have model A and B .

class A(ModelView):
datamodel = SQLAInterface(Tabel_A)

and

class B(ModelView):
datamodel = SQLAInterface(Table_B)

A has a action like :

@action("mularchive", "Archieve", "Archieve all Really?", "fa-rocket", single=False)
def mularchive(self, items):
    self.update_redirect()
    for item in items:
        new_item = item.__class__()
        new_item.name = item.name
        B.add(new_item)
    return redirect(self.get_redirect())

So I'm expect object b will save into table B. But this is not working and it complain about

AttributeError: 'Table A' object has no attribute 'method_permission_name'

please help, I'm new to flask appbuilder and python... not sure where I made mistake .

CD-Key
  • 63
  • 1
  • 7

1 Answers1

0
for item in items
    b = B(name = item.name)
    db.session.add(b)
    db.session.commit()
self.update_redirect()
return redirect(self.get_redirect())

CD-Key
  • 63
  • 1
  • 7
  • 1
    Thank you for taking the time to follow-up with an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts. – Jeremy Caney Oct 07 '21 at 16:13
  • For performance, it's better to have the `db.session.commit()` call happen after the for loop. That way you only write once, instead of writing X number of times, where X is the number of items in a list. – Klutch27 Oct 11 '22 at 16:54