python - Related name already in use by another Foreign key -
i've created small sqlite3 database using peewee try understand foreign keys , them working in simple database.
from peewee import * db = sqlitedatabase('database.db') class category(model): category = charfield() class meta: database = db class subcat(model): description = blobfield() sub_cat = charfield() top_category = foreignkeyfield(category, related_name='group') class meta: database = db db.connect() db.create_tables([category, subcat], safe=true)
i have created controller.py file handle database transactions. (updated) peewee import * database import *
class modcat(category): def add_cat(self,category): update = category.create(category=category) def get_cat(self): categories = category.select(category.category).order_by(category.category) return categories class modsubcat(subcat): def save_sub_cat(self, sub, master, desc): name = category().select().where(category.name==master) update = subcat.create(sub_cat=sub, top_category=name, description=desc)
finally, main.py allows me enter data database simple form created in wxformbuilder.
from gui import * controller import * class menu(indb): def __init__(self, parent): indb.__init__(self, parent) get_categories = modcat() list = get_categories.get_cat() new_list = [] thing in list: new_list.append(thing.category) print new_list f in new_list: self.m_combobox1.append(f) def click_save( self, event ): new_cat = modcat() new_cat.add_cat(self.m_textctrl3.getvalue()) self.getparent() # assigns parent frame frame. self.close() # closes frame removing main menu. frame = menu(none) frame.centre() frame.show() def sub_save( self, event ): sub = self.m_textctrl5.getvalue() master = self.m_combobox1.getvalue() desc = "hi" update = modsubcat() update.save_sub_cat(sub, master, desc) #start app calling sub class of mainmenu if __name__ == '__main__': app = wx.app(0) frame = menu(none) frame.centre() frame.show() app.mainloop()
the database creates no errors when run main.py returns error.
traceback (most recent call last): file "c:/users/********/pycharmprojects/rams/main.py", line 2, in <module> controller import * file "c:\users\********\pycharmprojects\rams\controller.py", line 13, in <module> class modsubcat(subcat): file "c:\python27\lib\site-packages\peewee.py", line 4710, in __new__ field.add_to_class(cls, name) file "c:\python27\lib\site-packages\peewee.py", line 1437, in add_to_class invalid('the related_name of %(field)s ("%(backref)s") ' file "c:\python27\lib\site-packages\peewee.py", line 1431, in invalid raise attributeerror(msg % context) attributeerror: related_name of modsubcat.top_category ("group") in use foreign key.
i've changed related_by value no luck. fact have sub class modsubcat in controller.py causing problem?
ive removed related_name='group'
from top_category = foreignkeyfield(category, related_name='group')
, works.
what related_name for?
Comments
Post a Comment