News for hackers. Stuff that matters.
Re:Django REST framework 3.7 released by theptip on Wednesday November 01, 2017 @04:05PM (#15604976)
The Django ORM and DRF autogenerated views are very good for quickly building CRUD APIs.

You get a lot of stuff in the box with Django:

* a very functional (if exceptionally complicated and hard-to-modify) admin UI

* a friendly ORM with good support for autogenerated DB schema migrations (better than SQLAlchemy for simple CRUD, but harder to go off the beaten track when you need to)

And building on that, DRF gives you

* Autogenerated CRUD API endpoints for your DB models

* Autogenerated serializers for your DB models, with field type validation

All of the above exists as plugins for Flask, but the feature set is less well integrated, and you need to figure out which plugin you want to include.

If you're trying to build a super-light microservice then Flask is probably worth looking at, though you can get Django quite light with a bit of work.

If you're trying to build a more complex system with nested API endpoints then DRF makes this harder for you (it's not possible in native DRF, but there's a plugin for it; I'm using this approach so it's not terrible).

And if you don't buy in to Django's ActiveRecord ORM approach (e.g. you want to do DDD and want to use a DataMapper style interface on your DB), then you need SQLAlchemy, and you can't use all the clever ORM mapping tools that Django/DRF brings, and so you may as well use Flask.

Re:Django REST framework 3.7 released by andybak on Wednesday November 01, 2017 @05:57PM (#15605869)
> exceptionally complicated and hard-to-modify

As an exceptionally mediocre programmer who routinely creates extensive admin customisations I dispute this.

Seriously. Where does this meme come from? The admin is remarkably easy to customize for the most part. It's biggest flaws are incomplete documentation and a few monolithic methods/templates mostly around changelists.

Re:Django REST framework 3.7 released by andkon on Wednesday November 01, 2017 @06:47PM (#15606182)
At least part of the problem is that it looks so, so weird, and it's really tightly bound to models. This is like the most customized admin i've made and it's still pretty simple, but it sure doesn't feel that easy to reason about when you look at it (it's an admin page that lets you link a specific admin-page input to a model, then display it in a list that you can add to):

  from django.contrib import admin
  from django.forms import Textarea
  from .models import *
  
  # Register your models here.
  
  class BotTextInline(admin.StackedInline):
	model = BotReplyContent
	extra = 1
	fieldsets = [
		(None, {'fields': ('message_type', 'text', ('template_text', 'example_text',), 'order',)}),
		('Actions and inputs', {'classes': ('collapse,'), 'fields': ('action_type', 'input_type',)})
	]
	# readonly_fields = ['input_type_ptr']

  class BotReplyAdmin(admin.ModelAdmin):
	fieldsets = [
		(None,               {'fields': ('state', 'intent', 'context', 'description', 'next_state',)}),
	]
	formfield_overrides = {
		models.CharField: {'widget': Textarea()}
	}

        list_display = ('state', 'intent', 'context', 'description',)
	save_as = True # lets you save the content on screen as a new id
	inlines = [BotTextInline]

  admin.site.register(BotReply, BotReplyAdmin)
```
Re:Django REST framework 3.7 released by Daishiman on Thursday November 02, 2017 @02:41AM (#15608318)
This is a trivial amount of customization.

Yes, you have to know what the class fields do. But you just saved yourself days of having to make your own CRUD view from scratch.

Re:Django REST framework 3.7 released by andkon on Thursday November 02, 2017 @03:01AM (#15608386)
Totally trivial. And yet, I still can barely parse what's going on in there.
Re:Django REST framework 3.7 released by Daishiman on Friday November 03, 2017 @02:25AM (#15617043)
But proportional to the docs and total customizability and sheer mass of code and functionality that happens.

You're getting free authentication for an admin + free UI + a fully customizable list view + customizable create/update view + delete with a useful confirmation dialog + trivial field search + trivial filters of all sorts include date drill-downs.

It's a huge amount of things to do by hand.

Re:Django REST framework 3.7 released by seanwilson on Wednesday November 01, 2017 @09:54PM (#15607215)
> Seriously. Where does this meme come from? The admin is remarkably easy to customize for the most part.

I haven't found this myself to be honest. Usually you have to trawl through several stackoverflow posts and evaluate several different ways to do things you would expect to be simple. I don't have many examples in mind right now except showing image thumbnails, dealing with base64 image uploads and automatically resizing image uploads were way more cumbersome to implement than I would have expected. Most solutions we arrived at felt hacky for tasks you would think would be built in by now but I don't have a better framework in mind for CRUD work.

The default admin interface is really good though as well as the database migration tooling.

Re:Django REST framework 3.7 released by theptip on Wednesday November 01, 2017 @07:27PM (#15606460)
My experience report is based on operating a production API with about 30 DB models, not incredibly complex, but with somewhat non-standard requirements like multi-tenancy. This opinion is shared by the developers on my team as well, if that's worth anything in addition.

I've found the easy stuff to be very easy to do in the Django admin, and every time I've tried to do something off the beaten track, it's taken me days instead of the hours or minutes it feels like it should do.

Any of these would be good examples of things that feel like they should be easy, but aren't:

* Adding a link to a custom page onto the frontpage of the admin, ideally under a custom "App" header. * Creating a read-only view onto related objects (impossible in some situations, depending on the direction of the FKey relationship, annoying in others). * Presenting a form for creating nested one-to-one objects along with a parent.

I'm sure that if you routinely make modifications to the Django Admin it gets easier; the second time on any of these items is 10-100 times quicker for me.

I'd blame the poor code discoverability of the template-based system, poor documentation, and lack of a broad corpus of examples for making this harder than it could be.

Re:Django REST framework 3.7 released by theli0nheart on Wednesday November 01, 2017 @09:16PM (#15607068)
> * Adding a link to a custom page onto the frontpage of the admin, ideally under a custom "App" header.

This one's simple—just override base_site.html.

As for the others, I'm not really sure I understand what creating a read-only view / form for nested 1-1 objects means, but I'm sure there's a decent, not so crazy solution out there.

Re:Django REST framework 3.7 released by Alex3917 on Wednesday November 01, 2017 @10:29PM (#15607370)
> just override base_site.html

And if your changes break in a future version of Django, how exactly is anyone going to be able to debug that? Having to override admin templates that are at best only semi-documented is incredibly hacky.

Re:Django REST framework 3.7 released by theli0nheart on Wednesday November 01, 2017 @10:40PM (#15607425)
With any Django app, you’re going to pin your requirements to a specific Django version as a matter of course. If the change is broken in a future Django version, you’d need to update it like you would need with any other breaking change.

But, if experience is any guide, despite lots of tinkering with the admin, Django has yet to break anything I’ve customized in the admin (I’ve been using Django since 0.96). YMMV.

Re:Django REST framework 3.7 released by Daishiman on Friday November 03, 2017 @02:54AM (#15617146)
Django has the best deprecation policy of any framework I've seen. Breaking changes are always documented and given fair advance for changes.

In many years of working with Django I have not really seen a significant breaking change in the admin unless you override private methods.

Re:Django REST framework 3.7 released by elnygren on Wednesday November 01, 2017 @07:48PM (#15606581)
If you are spending "days" in some Django Admin modifications, you are definitely doing it wrong. At that point you should have rolled out your own admin just like any other feature. Clearly the builtin admin is working against you rather than with you if it takes that long to do what you need to get done.

The Django admin is meant for development. Ofc, it can be used by non-devs and I've seen a multimillion dollar business that mainly uses the builtin admin to manage their main product. However, there's always that point when customising something that is fundamentally different than what your needs are doesn't make sense.

Re:Django REST framework 3.7 released by theptip on Wednesday November 01, 2017 @08:46PM (#15606892)
It would take considerably longer than a day or two to build a replacement admin of comparable functionality, so I don't agree with your assessment.
Re:Django REST framework 3.7 released by andybak on Sunday November 05, 2017 @06:29PM (#15632435)
> The Django admin is meant for development.

I dispute this. And the community is pretty split on it too.

Re:Django REST framework 3.7 released by 3131s on Wednesday November 01, 2017 @09:47PM (#15607185)
I recently overrode the admin widget for ArrayField to make it possible to add and delete entries individually rather than entering the whole array as a big comma-separated block. I got it working but it felt pretty hackish. Maybe there was a better way but I had trouble finding good resources on what I wanted to do.
Re:Django REST framework 3.7 released by andybak on Sunday November 05, 2017 @06:30PM (#15632439)
Surely that's Django forms and widgets? Nothing there is admin-specific.
Re:Django REST framework 3.7 released by stuaxo on Thursday November 02, 2017 @10:54AM (#15610920)
Have had the same hacky feeling trying to do anything in the admin beyond using the standard widgets.
Re:Django REST framework 3.7 released by pmiller2 on Thursday November 02, 2017 @01:51AM (#15608138)
OTOH, using Django + DRF, you will be fighting the framework if you want to do anything besides serialize and deserialize models from your database. We use DRF where I'm at, and I often wish we didn't.
Re:Django REST framework 3.7 released by Daishiman on Thursday November 02, 2017 @02:37AM (#15608308)
If you're fighting the framework, you're doing it wrong.

It is trivial to use serializers for non-ORM stuff. It is trivial to not use serializers at all. It is trivial to plug your own auth and permissions.

Re:Django REST framework 3.7 released by StavrosK on Thursday November 02, 2017 @05:35AM (#15608953)
I'm a heavy Django user, and have used DRF occasionally. I find Django extremely pluggable and extensible, but I needed to write an API with DRF that didn't map cleanly onto the database models, and that was pretty cumbersome.

DRF doesn't like it when you do things that don't map to the models, and the documentation is pretty bad. I had to read the source in the end to figure out how to do what I wanted, and I got the sense that I was lucky it was doable.

If your database models map to your CRUD endpoints, though, everything is super easy.

Re:Django REST framework 3.7 released by theptip on Thursday November 02, 2017 @01:48PM (#15612553)
I use a lot of custom Serializers (not tied to any Model), and lots of SerializerMethodFields to selectively override generated ModelSerializer fields; this can get you quite far.

I agree with your point that the further afield you go, the less you gain from the framework, though.

Re:Django REST framework 3.7 released by StavrosK on Thursday November 02, 2017 @02:00PM (#15612666)
That's what I ended up doing, if I recall correctly, but the docs on this were pretty much non existent.
Re:Django REST framework 3.7 released by Daishiman on Wednesday November 01, 2017 @05:22PM (#15605598)
> a very functional (if exceptionally complicated and hard-to-modify) admin UI

The flexibility of the admin UI has improved dramatically in the last few releases. It's still relatively complicated, but it now has way more places to override behavior.

Re:Django REST framework 3.7 released by andybak on Wednesday November 01, 2017 @05:58PM (#15605880)
> last few releases

By "last few" you mean "since about Django 1.4" in 2012