It would be better if I give an example that I was working on. The Models are
class Author(models.Model):
id = models.IntegerField(db_column='a_id', primary_key=True)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
class Book(models.Model):
id = models.IntegerField(db_column='b_id', primary_key=True)
title = models.CharField(max_length=60)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
And the Serializers are,
class AuthorSerializer(ModelSerializer):
class Meta:
model = Author
fields = ('id', 'first_name', 'last_name')
class BookSerializer(ModelSerializer):
class Meta:
model = Book
fields = ('id', 'author', 'title')
And the view sets are,
class AuthorViewSet(NestedViewSetMixin, ModelViewSet):
serializer_class = AuthorSerializer
queryset = Author.objects.all()
class BookViewSet(NestedViewSetMixin, ModelViewSet):
serializer_class = BookSerializer
queryset = Book.objects.all()
Now The following APIs are working okay,
/authors/
/authors/{author_id}/
/authors/{author_id}/books/
However, If I change the Author Views to
class AuthorViewSet(NestedViewSetMixin, ModelViewSet):
serializer_class = AuthorSerializer
queryset = Author.objects.all()
lookup_field = 'first_name'
Then, the following APIs work
/authors/
/authors/{author_first_name}/
But the following doesn't work.
/authors/{author_first_name}/books/
I understand that it's because of the Foreign key relations between these two tables. I was just wondering whether there is any workaround if I want to keep the lookup_fields.
Thanks