nonrel with mongodb relations
I'm converting django with MySQL to django-nonrel with mongodb. The biggest issue is the many to many relations. I saw that ListField of models.ForeignKey doesn't work as expected.
Lets say that a Person can write many Posts, and each Post can be written by many Persons. So the solution I've now seems to me really not efficient:
class Person(models.Model):
first_name = models.TextField()
posts = ListField(models.ForeignKey('Post'))
def get_posts(self):
posts_list = []
for post_oid in self.posts:
author = Post.objects.get(id=post_oid)
posts_list.append(author)
return posts_list
class Post(models.Model):
text = models.TextField()
authors = ListField(models.ForeignKey(Person))
def add_author(self, person):
if self.has_author(person):
return
person.posts.append(ObjectId(self.id))
person.save()
self.authors.append(ObjectId(person.id))
self.save()
def get_authors(self):
authors_list = []
for person_oid in self.authors:
author = Person.objects.get(id=person_oid)
authors_list.append(author)
return authors_list
def has_author(self, person):
return ObjectId(person.id) in self.authors
what is the right way to do it?
Thanks!
链接地址: http://www.djcxy.com/p/71978.html下一篇: nonrel与mongodb关系