daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest Sep 26th, 2016 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Assignee(EmbeddedDocument):
  2.     id = ObjectIdField(primary_key=True)
  3.     assignee_email = EmailField(required=True)
  4.     assignee_first_name = StringField(required=True)
  5.     assignee_last_name = StringField()
  6.     assignee_time = DateTimeField(required=True, default=datetime.datetime.utcnow)
  7.     user = ReferenceField('MongoUser', required=True)
  8.     user_id = ObjectIdField(required=True)
  9.  
  10. class MongoUser(Document):
  11.     email = EmailField(required=True, unique=True)
  12.     password = StringField(required=True)
  13.     first_name = StringField(required=True)
  14.     last_name = StringField()
  15.     assignees= EmbeddedDocumentListField(Assignee)
  16.    
  17. class MongoUserSerializer(DocumentSerializer):
  18.     assignees = AssigneeSerializer(many=True)
  19.     class Meta:
  20.         model = MongoUser
  21.         fields = ('id', 'email', 'first_name', 'last_name', 'assignees')
  22.         depth = 2
  23.  
  24. class AssigneeSerializer(EmbeddedDocumentSerializer):
  25.     class Meta:
  26.         model = Assignee
  27.         fields = ('assignee_first_name', 'assignee_last_name', 'user')
  28.         depth = 0
  29.    
  30. class AssigneeSerializer(EmbeddedDocumentSerializer):
  31.     class Meta:
  32.         model = Assignee
  33.         fields = ('assignee_first_name', 'assignee_last_name', 'user_id')
  34.         depth = 0
  35.    
  36. fields = ('assignee_first_name', 'assignee_last_name', 'user_id')
  37.    
  38. from rest_framework_mongoengine.viewsets import ModelViewSet
  39.  
  40. class MongoUserViewSet(ModelViewSet):
  41.  
  42.     def get_queryset(self):
  43.         queryset = MongoUser.objects.all()
  44.         # Set up eager loading to avoid N+1 selects
  45.         queryset.select_related(max_depth=3)  
  46.        return queryset
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand