我可以以某种方式在Django ManyToMany字段中使用to_field参数吗? [英] Can I somehow use the to_field parameter in a Django ManyToMany field?

查看:1167
本文介绍了我可以以某种方式在Django ManyToMany字段中使用to_field参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django的 ForeignKey 字段中,我可以指定 to_field 。这是伟大的,因为我可以使用slugs作为外键,并做相当于'自然键'的功能。

In Django's ForeignKey field I can specify the to_field. This is great, as I can use slugs as foreign keys and do the equivalent of the 'natural key' functionality.

我在 ManyToMany 中找不到任何这样的字段。我不明白为什么不这样。有没有办法做到这一点?也许在我自己的m2m连接表中明确地使用通过

I can't find any such field in ManyToMany. I don't see why not. Is there some way to do this? Perhaps do it explicitly in my own m2m join table with through?

推荐答案

我不认为Django对 ManyToManyField 的内置 to_field 参数。但是,像你所说的那样,你可以通过表格手工创建一个,这显然会使用 ForeignKey 字段,它们支持 to_field 参数。

I don't think Django has a build-in to_field parameter for ManyToManyField. However like you mentioned you can manually make a through table, which obviously will use ForeignKey fields, which do support to_field parameter.

不是一个干净的方法,不遵循DRY,但我不认为有任何更好的方式来解决这个问题。也许你应该提交一个功能请求。

Not really a clean method and does not follow the DRY, but I don't think there is any better way of currently solving this problem. Perhaps you should file a feature request for this.

这篇关于我可以以某种方式在Django ManyToMany字段中使用to_field参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆