I've got a model "Assignment" that I want to be able to update through the api.
Updated per comments
urls.py
router = routers.DefaultRouter()
router.register(r'assignments', views.AssignmentList, base_name='Assignments')
urlpatterns = [
url(r'^api/v1/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
serializers.py
class AssignmentSerializer(serializers.ModelSerializer):
class Meta:
model = Assignment
fields = (
'id', 'company', 'farm', 'sensor', 'name', 'location', 'notes', 'units', 'max_height', 'frequency',
'interval', 'max_threshold', 'min_threshold', 'notify', 'last_alert_time', 'last_alert_water_level')
views.py
class AssignmentList(viewsets.ModelViewSet):
serializer_class = AssignmentSerializer
pagination_class = None
def get_queryset(self):
queryset = Assignment.objects.all()
company_id = self.request.query_params.get('company_id', None)
sensor_id = self.request.query_params.get('sensor_id', None)
if company_id is not None:
queryset = Assignment.objects.filter(company_id=company_id)
if sensor_id is not None:
queryset = Assignment.objects.filter(sensor_id=sensor_id)
return queryset
Currently my view allows for easy filtering based on two of the fields, 'company_id' and 'sensor_id'. This allows for easy access of the data in json. Unfortunately I can't figure out how to POST back even with the built in API form. I would like to be able to filter down to a single instance and edit a single field, let's say "assignment.name" for now.
It's my understanding that...
The actions provided by the ModelViewSet class are .list(), .retrieve(), .create(), .update(), .partial_update(), and .destroy(). (DRF Docs)
So what do I need to do to leverage them to edit a model instance via url? Or honestly just edit one period. I've been running in circles trying different Mixins, views (UpdateAPIView, RetrieveUpdateAPIView etc.) and this question in particular Stack Overflow: Django Rest Framework update field.