Checklist of things to update in Django when making a new resource:
- add model to appropriate models.py file
- add migration to appropriate application – python manage.py makemigrations
- add serializer to app/serializers.py
- add ViewSet to app/apiviews.py or app/views.py
- add route to router – apirouter.py or urls.py
- add admin to app/admin.py
- add Filter (if using django_filters) to app/filters.py
- update templates in app/templates/
- 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(r‘ROUTE‘, 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’]
}