Django – New resource

Checklist of things to update in Django when making a new resource:

  1. add model to appropriate models.py file
  2. add migration to appropriate application – python manage.py makemigrations
  3. add serializer to app/serializers.py
  4. add ViewSet to app/apiviews.py or app/views.py
  5. add route to router – apirouter.py or urls.py
  6. add admin to app/admin.py
  7. add Filter (if using django_filters) to app/filters.py
  8. update templates in app/templates/
  9. add test in app/tests.py

 

3 – Serializer

class AppModelSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.MODELNAME

4 – ViewSet

class AppModelViewSet(viewsets.ModelViewSet):
queryset = models.MODELNAME.objects.all()
serializer_class = serializers.MODELSERIALIZER
filter_fields = (‘name’, ‘slug’)

5 – Router

router.register(rROUTE, APP_VIEWS.AppModelViewSet)

6 – Admin

class AppModelAdmin(admin.ModelAdmin):
list_display = (‘name’, ‘slug’, ‘order’)

admin.site.register(models.AppModel, AppModelAdmin)

7 – Filter

class AppModelFilter(django_filters.FilterSet):
class Meta:
model = models.AppModel
fields = [‘name’, ‘slug’]

OR

class AppModelFilter(django_filters.FilterSet):
class Meta:
model = models.AppModel
fields = {

‘name’: [ ‘exact’, ‘startswith’, ‘icontains’],

‘other_reference’: [‘exact’]

}

Leave a comment