I'm using Django 2.0 and Django REST Framework.
I have two models in contacts app
contacts/models.py
class Contact(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100, blank=True, null=True, default='')
class ContactPhoneNumber(models.Model):
contact = models.ForeignKey(Contact, on_delete=models.CASCADE)
phone = models.CharField(max_length=100)
primary = models.BooleanField(default=False)
def __str__(self):
return self.phone
contacts/serializers.py
class ContactPhoneNumberSerializer(serializers.ModelSerializer):
class Meta:
model = ContactPhoneNumber
fields = ('id', 'phone', 'primary', 'created', 'modified')
and contacts/views.py
class ContactPhoneNumberViewSet(viewsets.ModelViewSet):
serializer_class = ContactPhoneNumberSerializer
def get_queryset(self):
return ContactPhoneNumber.objects.filter(
contact__user=self.request.user
)
urls.py
router.register(r'contact-phone', ContactPhoneNumberViewSet, 'contact_phone_numbers')
What I want is following endpoints
- GET:
/contact-phone/{contact_id}/list phones numbers of particular contact - POST:
/contact-phone/{contact_id}/add phone numbers to particular contact - PUT:
/contact-phone/{contact_phone_number_id}/update particular phone number - DELETE:
/contact-phone/{contact_phone_number_id}/delete particular phone number
PUT and Delete can be achieved as default action of ModelViewSet but how to make get_queryset to accept contact_id as required parameter?
Edit 2
I followed doc Binding ViewSets to URLs explicitly
update app/urls.py
router = routers.DefaultRouter()
router.register(r'contacts', ContactViewSet, 'contacts')
contact_phone_number_view_set = ContactPhoneNumberViewSet.as_view({
'get': 'list/<contact_pk>/',
'post': 'create/<contact_pk>/',
'put': 'update',
'delete': 'destroy'
})
router.register(r'contact-phone-number', contact_phone_number_view_set, 'contact_phone_numbers')
urlpatterns = [
path('api/', include(router.urls)),
url(r'^admin/', admin.site.urls),
]
But it is giving error
AttributeError: 'function' object has no attribute 'get_extra_actions'