Ask coding questions

← Back to all posts
How do I insert a user-defined class into a database value?
barnesrd (0)

I'm making a directory for registered users' profile data but I keep getting a circular reference error when I process this line:

db["Users"] = User(ctx.author.name, 0, 0, 0, db["Table"])
(Line 59 since for some reason the whole line isn't showing)

The User class is a simple class with only member variables which are defined by the constructor.

Answered by Coder100 (18228) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18228)

you can't, you can only store python values, like:
int, dict, str, bool, and so on. Anything that can be parsed as json. So add a new method called to_json

like:

def to_json(self):
  return { '<key>': self.<key>, ... }

and just add all the keys you want to save.

Then, you can turn the json back like this:

data = db["..."]
user = User(data["..."], data["..."])