Live data from Hacker News

Django REST framework 3.5

django-rest-framework.org

21–30 of 68 posts

Re: Django REST framework 3.5

#21

I always miss DJRF when using languages other than Python

isnt DRF built into rails basically (never used rails)

Last time I checked, in Rails there's no Controller class that has all the built-in functionality of a DRF ViewSet. For example, you still have to perform the DB queries for every action yourself, rather than just define a queryset and serializer for the entire class and let the framework handle the rest (no pun intended).

Re: Django REST framework 3.5

#22
post #20

There’s no need for a framework since you can do: values = User.objects.all().values('id', 'username') results = json.dumps(values) …or use the builtin JSON serializer. You can also use Paginator to paginate the results.

Why use a website - just have your users call you and ask you to do things.

Re: Django REST framework 3.5

#23
post #20

There’s no need for a framework since you can do: values = User.objects.all().values('id', 'username') results = json.dumps(values) …or use the builtin JSON serializer. You can also use Paginator to paginate the results.

That's a small chunk of the functionality most APIs need. Why write all of your API views from scratch when there's an excellent framework that handles it for you?

Re: Django REST framework 3.5

#24
post #14

Earlier quoted context omitted.

Data structures used internally for processing are something totally different than data structures used for communication with outside world. Usually you don't want to expose internals with public API, and even when you do, you want to be very selective about what is shown and how. When you publish internals, you only cement your implementation so you can't change that easily later (e.g. when you realize how crappy…

I agree, I find doing this with DRF very easy and had the opposite with TastyPie doing stuff I did not want or need. Having some ORM integration is nice; there are a number of situations where it's easy to use your ORM safely and get stuff done very quickly (eg. basic CRUD stuff for instance)

"Basic CRUD" is basic only in most trivial situations, which by themselves are rare. Much more often you need to create a set of related objects. With this "basic CRUD" you have many points where network can break, and network programming has one very important principle: network always breaks, so the program has to survive in such case.

Re: Django REST framework 3.5

#25
post #16

I have a question about http://www.django-rest-framework.org/tutorial/2-requests-and... Is it a good practice to handle all verbs in the function dealing with the particular request as shown in the example? I think for a real world scenario that would be a mess to read, but for a less experienced developer this could hint that such approach is alright and result in a less readable code base in the future.

For a typical setup, you can just use the built in Routers. It's a much cleaner way to handle all the verbs. You can always override functions if you need it to do something special.

http://www.django-rest-framework.org/api-guide/routers/

Re: Django REST framework 3.5

#26
post #7
post #4

For people who are unfamiliar, there is also Tastypie. I find it's Model centric approach a whole lot nicer to work with then Django-Rest .

Which is basically stupid by itself, as you simply expose your internal data structures without control or thought.

I'm guessing you haven't tried it. TastyPie gives you complete control and customization for all parts of your api.

Re: Django REST framework 3.5

#27
post #16

I have a question about http://www.django-rest-framework.org/tutorial/2-requests-and... Is it a good practice to handle all verbs in the function dealing with the particular request as shown in the example? I think for a real world scenario that would be a mess to read, but for a less experienced developer this could hint that such approach is alright and result in a less readable code base in the future.

If you proceed with the tutorial it is going to show you what I consider is the cleanest way to go for views in django rest framework (ViewSets, coupled with the routers, part 6 of the tutorial).

Basically, you will 1. split verbs to different methods of one class and 2. you can not implement verbs you are not going to allow (you can of course do that also with the functional style you see in the link you posted)

Re: Django REST framework 3.5

#28
post #27
post #16

I have a question about http://www.django-rest-framework.org/tutorial/2-requests-and... Is it a good practice to handle all verbs in the function dealing with the particular request as shown in the example? I think for a real world scenario that would be a mess to read, but for a less experienced developer this could hint that such approach is alright and result in a less readable code base in the future.

If you proceed with the tutorial it is going to show you what I consider is the cleanest way to go for views in django rest framework (ViewSets, coupled with the routers, part 6 of the tutorial). Basically, you will 1. split verbs to different methods of one class and 2. you can not implement verbs you are not going to allow (you can of course do that also with the functional style you see in the link you posted)

Having maintained a DRF-based API before, +1 to using ViewSets.

Re: Django REST framework 3.5

#29
post #4

For people who are unfamiliar, there is also Tastypie. I find it's Model centric approach a whole lot nicer to work with then Django-Rest .

Ditto with everyone else. Tastypie is unpleasant to build upon, except for the simple examples of their tutorial, and has rightfully been subsumed by DRF.

Re: Django REST framework 3.5

#30
I like DRF, but I think its allow-by-default mentality is a security risk. If you use it, keep in mind that:

* ModelViewSet is read/write, you should use the more verbose ReadOnlyModelViewSet until you know you want to allow writing.

* Fields specified in the "fields" member are read/write by default. You have to explicitly declare the field on the ViewSet and pass "readonly=true" to make it read-only. This is especially dangerous for ForeignKey fields, which can be used to change object ownership if you aren't careful.

When I was responsible for a DRF-based API I wrote some custom Fields and ViewSets to use safer defaults, and I recommend others do the same.

Post reply on HN