Newbie in Django REST framework and seems I'm missing some basics. Have read back and forth hundreds of questions here and on the web, yet can't find where the issue is.
The aim is to have well linked and easy to explore REST api (clickable), like an example demonstrated as part of https://github.com/encode/rest-framework-tutorial.
Any hints are more than welcome, as I'm pulling my hairs and can't find a working solution.
class GroupManager(models.Manager):
use_in_migrations = True
def get_by_natural_key(self, name):
return self.get(name=name)
class Group(models.Model):
name = models.CharField(_('name'), max_length=80, unique=True)
permissions = models.ManyToManyField(
Permission,
verbose_name=_('permissions'),
blank=True,
)
objects = GroupManager()
class Meta:
verbose_name = _('group')
verbose_name_plural = _('groups')
def __str__(self):
return self.name
def natural_key(self):
return (self.name,)
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ('url', 'name')
class GroupViewSet(viewsets.ModelViewSet):
queryset = Group.objects.all()
serializer_class = GroupSerializer
Above results in very nice linked REST api with automatically generated URL which is clickable and takes to instance details:
Trying to replicate it with my very simple model defined as below fails misreably:
class Author(models.Model):
name = models.CharField("Authorist", max_length=20)
def get_by_natural_key(self,name):
return self.get(name=name)
def __str__(self):
return self.name
def natural_key(self):
return (self.name,)
class Book(models.Model):
title = models.CharField("Tytuł",max_length=100)
autorek = models.ForeignKey(Author,related_name='knicha',on_delete=models.CASCADE)
class AuthorSerializer(serializers.ModelSerializer):
# id = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='author-detail')
class Meta:
model = Author
# fields = ('__all__')
fields = ('url','name')
# depth = 2
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ('__all__')
depth = 2
class AuthorVS(viewsets.ModelViewSet):
queryset = Author.objects.all()
serializer_class = AuthorSerializer
class BookVS(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
router.register('author',AuthorVS)
router.register('book',BookVS)
Error produced is:
Could not resolve URL for hyperlinked relationship using view name "author-detail". You may have failed to include the related model in your API, or incorrectly configured the
lookup_field
attribute on this field.
If anyone can explain what and why needs to be done for my example model, that would be grant. I did read through HyperLinkedModels, etc. but can't get this working. I'm pretty sure I'm missing some silly bit to get it working.
{ "author": "http://localhost:8000/test/author/", "book": "http://localhost:8000/test/book/" }
. All of them works:[ { "url": "http://localhost:8000/test/author/1/", "name": "abcd" } ]
and[ { "id": 1, "title": "efg", "autorek": { "id": 1, "name": "abcd" } } ]
– Hai Langauthor-detail
in the line# id = serializers...
. Why did you comment it out? When I uncommented it, I received different error than yours. Could you please update your question with the version that has the error? – Hai Lang