Я использую Django rest framework. В сериализаторе я использовал HyperlinkedModelSerializer, но есть некоторые поля внешнего ключа с подробным просмотром, поэтому он может быть отображен как URL, но некоторые из них этого не делают, он выдает мне следующую ошибку:
Could not resolve URL for hyperlinked relationship using view name "unit-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field
Так как это исправить?
serializers.py
class MaterialSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Material
fields = ('id', 'url', 'name', 'location', 'category', 'unit', 'type', 'price',)
views.py
class MaterialViewSet(viewsets.ModelViewSet):
queryset = Material.objects.all()
serializer_class = MaterialSerializer
models.py
class Material(models.Model):
name = models.CharField(max_length=200)
location = models.ForeignKey(Location, null=True, on_delete=models.CASCADE)
category = models.ForeignKey(Category, null=True, on_delete=models.CASCADE)
unit = models.ForeignKey(Unit, null=True, on_delete=models.CASCADE)
type = models.CharField(choices=TYPE_CHOICES, default='material', max_length=200)
price = models.FloatField(default=0, blank=True, null=True)
urls.py
router = DefaultRouter()
router.register('users', user_views.UserViewSet, 'users')
router.register('profiles', profile_views.ProfileViewSet, 'profile')
router.register('location', LocationViewSet, 'location')
router.register('category', CategoryViewSet)
router.register('materials', MaterialViewSet)
router.register('supplier', SupplierViewSet)
router.register('transaction', TransactionViewSet)
urlpatterns = [
path('v1/', include(router.urls)),
]
Итак, в моем API я хочу, чтобы мои поля внешнего ключа «location» и «category» имели только URL и поле «unit», id