Documentation on ViewSets

41 views
Skip to first unread message

Patrick Tchankue

unread,
May 12, 2015, 3:41:34 AM5/12/15
to django-res...@googlegroups.com
Hi there!
I started using ViewSets recently, I went through the documentation but I can't do a couple of things that I need to do:
1) When presented with the form (POST), all fields are required despite the fact that I specified that some fields should not be required in the serializer,
# views.py
class PersonViewSet(viewsets.ModelViewSet):
serializer = PersonSerializer(data=request.data)
if serializer.is_valid():

post = Person()
                        ......

2) This is just a comment: I use databases that are not directly supported by Django (Neo4J, Cassandra), my understanding is that I should override class methods (list, create, retrieve, update, destroy). Am I right?

Please give me some ideas on how to approach these issues or a practical tutorial that I can follow.

Regards,
Patrick

Filipe Ximenes

unread,
May 13, 2015, 1:08:45 PM5/13/15
to django-res...@googlegroups.com
Hi,

On Tue, May 12, 2015 at 4:41 AM, Patrick Tchankue <ptch...@gmail.com> wrote:
Hi there!
I started using ViewSets recently, I went through the documentation but I can't do a couple of things that I need to do:
1) When presented with the form (POST), all fields are required despite the fact that I specified that some fields should not be required in the serializer,
# views.py
class PersonViewSet(viewsets.ModelViewSet):
serializer = PersonSerializer(data=request.data)
if serializer.is_valid():

post = Person()
                        ......


This code does not looks right. Could you send you full "views.py" file in a gist?
 
2) This is just a comment: I use databases that are not directly supported by Django (Neo4J, Cassandra), my understanding is that I should override class methods (list, create, retrieve, update, destroy). Am I right?

It you are not using Django's ORM, you will probably have some trouble with model serializers and saving data. ViewSets are an abstraction that assume the use of model serializers, you have to think this through since model serializers may not work for you. It may be the case you need to start from APIView and build your methods "manually".
 

Please give me some ideas on how to approach these issues or a practical tutorial that I can follow.

Regards,
Patrick

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
  
Filipe Ximenes
+55 (81) 8245-9204
Vinta Software Studio
http://www.vinta.com.br

Patrick Tchankue

unread,
May 14, 2015, 3:18:26 AM5/14/15
to django-res...@googlegroups.com
Hi Filipe,

The following code has the same issue as the other: I have to provide all fields in order to post a new City
# models.py
class City(models.Model):
name = models.CharField(max_length=25)
country = models.CharField(max_length=25)
population = models.IntegerField(default=0)
def __str__(self):
return self.name + " is in " + self.country

# serializers.py
class CitySerializer(serializers.ModelSerializer):

class Meta:
model = City 

# urls.py
city_viewset = CityViewSet.as_view({
    'get': 'list',
    'post': 'create',
})

city_detail = CityViewSet.as_view({
    'get': 'retrieve',
    'put': 'update',
    'delete': 'destroy'

})

urlpatterns = patterns('',

url(r'^api/city/$', city_viewset, name='cities'),

url(r'^api/city/(?P<pk>[0-9]+)/$', city_detail, name='city-detail'),
)

I implemented a walk around this issue which consists on implicitly defining the serializers and for non required fileds I put "allow_null=True".


Regards,
Patrick

--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/Z2AGFu7zCNE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages