How do I save a list to a Peewee model?
I have created a model using peewee ORM that looks like this:
class Person(Model):
username = CharField(max_length=255, unique=True)
badges = ??? # No list field?
class Meta:
database = db
This is the data that I will be storing:
people = [
{'username': 'user1', 'badges': ['badge 1','badge 2']},
{'username': 'user2', 'badges': ['badge 1', 'badge 2', 'badge 3']},
{'username': 'user3', 'badges': ['badge 1', 'badge 2', 'badge 3', 'badge 4']},
]
I wanted to keep my icons in a list of strings, but there is no list box. How else can I do something like this?
+3
source to share
1 answer
You also need to define the model of the icon and define the relationship between the icon and the person. I think what you are looking for is a many-to-many relationship. You can define it like this and you have to use your queries to join tables.
class Person(Model):
username = CharField(max_length=255, unique=True)
class BadgeEarned(Model):
awardee = ForeignKey(Person)
badge = ForeignKey(Badge)
class Badge(Model):
description = Charfield(max_length=255, unique=True)
An example of a query with this ratio
query = (Person
.select()
.join(BadgeEarned)
.join(Badge))
Edit: There is also a lot for many plugins if you want to avoid having to write connections.
+5
source to share