Wednesday 15 May 2013

Getting mongoengine and django rest framework to play nice -


I am wondering how I can work together with each other and djangoRESTframework to work with each other Can get it. Currently, my model

  from Melboksing import * imported periodically class blog (document): post_id = IntField (unique = true) title = Stringfild (max_length = 144, required = true) date_created = DateTimeField (Default = datetime.datetime.now) body = StringField (required = true)   

and I have defined the serializer

  rest_framework Lists all blog posts from the .views import, or creates a new post "def" (self, request, format = none): post = blog   

But I error'm getting takes

  typeError at / blog / __init __ () that one has been argued (2) Request Method: GET Request URL : Http://127.0.0.1:8000/blog/ Django Version: 1.5.1 Exception Type: Type Exception Value: __init __ () takes exactly 1 argument (2 given)   

which gives the following Tresebak

  response = callback (request, * callback_args, ** callback_kwargs)    

The purpose of the Django REST framework is Dja To work well with the ngo model, there is no replacement for the MongoEngine Django model, as you will need to define some more functions to play well with the Django Rest Framework.

If you need to create a framework for MongoEngine, then take a look at this instead.

If you prefer to use the DJJAA Rest Framework, it is entirely possible, although your blog list class is actually a view, not a serializer, first of all Define the serial category:

Since the MongoEngine document is not a Django model example, you can call yourself serializers.ModelSerializer . This is the reason that restore_object is here from: Looking at a dictionary of deserialized field values, [this] will update the existing model example, or create a new model example.

Then you can define your idea, eg.

 From  Rest_framework Import From Generics Models import blog category bloglist (generics.ListCreateAPIView): serializer_class = blogservalizer def get_queryset (self): return blog. Objs   

Again, Django REST framework expects some of the standard Django models that are not verified by the mongo-engine documents, so redefine the get_queryset method need to.

then add to your urls.py:

  url (r '^ blog /', bloglist.as_view (), name = 'blog-list'), < / Code>   

No comments:

Post a Comment