I want to create a table where two of its fields combine to form an index field. My Python code for creating the table is as follows. What I want to do is make the combined fields course_name
and group_name
unique so that no two groups with the same course_name
and group_name
can be created. Can someone please help me with this?
JavaScript
x
5
1
class SocialGroup(Document):
2
timestamp = DateTimeField(default=datetime.now)
3
course_name = StringField()
4
group_name = StringField(choices=[('A', 1), ('B', 1), ('C', 1),('D', 1), ('E', 1), ('F', 1), ('None',1)], default="None")
5
Advertisement
Answer
You can specify indexes in the meta
dict of the class:
JavaScript
1
10
10
1
class SocialGroup(Document):
2
timestamp = DateTimeField(default=datetime.now)
3
course_name = StringField()
4
group_name = StringField(choices=[('A', 1), ('B', 1), ('C', 1),('D', 1), ('E', 1), ('F', 1), ('None',1)], default="None")
5
meta = {
6
'indexes': [
7
{'fields': ('course_name', 'group_name'), 'unique': True}
8
]
9
}
10