回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在尝试序列化我的DRF项目中的模型。该模型包含2个外键字段和一些简单的数据类型字段。其中一个外键字段是我的<code>CustomUser</code>模型,该字段在我的API输出中显示为一个超链接,这是预期的。另一个外键字段指向不同的模型(如下所示)。在过去的几个小时里,我一直在重复同样的几个错误,因为我尝试了其他StackOverflow线程中推荐的各种解决方案</p>
<p><strong>有人能告诉我如何将第二个外键作为链接显示在API输出中吗?</strong></p>
<p>当我的代码文件如下所示时,我在尝试访问<code>localhost:8000/trade/create/</code>和<code>localhost:8000/trader-accounts/</code>URL时出现此错误:</p>
<pre class="lang-py prettyprint-override"><code>django.core.exceptions.ImproperlyConfigured: Could not resolve URL for hyperlinked relationship using view name "traderaccount-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
</code></pre>
<p>当然,我在谷歌上搜索了好几次这个错误消息及其部分内容,还下了好几个DRF兔子洞尝试各种各样的东西</p>
<p><strong>交易/模型.py</strong></p>
<pre class="lang-py prettyprint-override"><code>class Trade(models.Model):
trade_owner = models.ForeignKey(CustomUser, related_name='owners', on_delete=models.RESTRICT)
trade_account = models.ForeignKey(TraderAccount, related_name='trades', on_delete=models.RESTRICT)
</code></pre>
<p><strong>交易员账户/模型.py</strong></p>
<pre class="lang-py prettyprint-override"><code>class TraderAccount(models.Model):
account_owner = models.ForeignKey(
CustomUser, on_delete=models.CASCADE,
)
account_name = models.CharField(
max_length=200, blank=True, verbose_name='Account Name'
)
</code></pre>
<p><strong>api/serializers.py</strong></p>
<pre class="lang-py prettyprint-override"><code>class TraderAccountSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = TraderAccount
fields = ['account_owner', 'account_name',]
class TradeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Trade
fields = ['trade_owner', 'trade_account', 'magic_number',]
</code></pre>
<p><strong>api/views.py</strong></p>
<pre class="lang-py prettyprint-override"><code>class TraderAccountView(generics.ListCreateAPIView):
permission_classes = (IsAuthenticated,)
queryset = TraderAccount.objects.all()
serializer_class = TraderAccountSerializer
class TradeCreateView(generics.ListCreateAPIView):
permission_classes = (IsAuthenticated,)
queryset = Trade.objects.all()
serializer_class = TradeSerializer
</code></pre>
<p><strong>api/url.py</strong></p>
<pre class="lang-py prettyprint-override"><code>
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
urlpatterns = [
path('', include(router.urls)),
path('trader-accounts/', views.TraderAccountView.as_view(), name='trader-accounts'),
path('trade/create/', views.TradeCreateView.as_view(), name='trade-create'),
</code></pre>
<hr/>
<p>当我从<code>api.serializers.TradeSerializer.Meta.fields</code>中删除<code>'trader_account'</code>时,我可以成功访问<code>localhost:8000/trade/create/</code>端点和<code>localhost:8000/trader-accounts/</code>端点</p>
<p><strong>api/serializers.py</strong></p>
<pre class="lang-py prettyprint-override"><code># Omit TraderAccountSerializer class for brevity in this example
class TradeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Trade
fields = ['trade_owner', 'magic_number',] # Removed 'trader_account'
</code></pre>
<p>我已经看了这么久了,现在我找不到错误。非常感谢你的帮助</p>