如何在 Django 中创建用户定义字段
好的,我正在开发一个具有多个不同模型的 Django 应用程序,即帐户、联系人等,每个模型都有一组不同的字段。除了现有字段之外,我需要能够允许每个用户定义自己的字段。我已经看到了几种不同的方法来实现这一点,从拥有大量的自定义字段到仅将自定义名称映射到每个用户使用的每个字段。我还提出了关于实现复杂映射或用户定义字段的 XML/JSON 样式存储/检索的建议。
所以我的问题是,有人在 Django 应用程序中实现了用户定义的字段吗?如果是这样,您是如何做到的以及您在整体实施方面的经验(稳定性、性能等)如何?
更新:我的目标是允许每个用户创建 n 个每种记录类型(帐户、联系人等),并将用户定义的数据与每个记录相关联。例如,我的一个用户可能希望将 SSN 与他的每个联系人相关联,因此我需要为他创建的每个联系人记录存储该附加字段。
谢谢!
标记
Ok, I am working on a Django application with several different models, namely Accounts, Contacts, etc, each with a different set of fields. I need to be able to allow each of my users to define their own fields in addition to the existing fields. I have seen several different ways to implement this, from having a large number of CustomFields and just mapping a custom name to each field used by each user. I have also seem recommendations for implementing complex mapping or XML/JSON style storage/retrieval of user defined fields.
So my question is this, has anyone implemented user defined fields in a Django application? If so, how did you do it and what was your experience with the overall implementation (stability, performance, etc)?
Update: My goal is to allow each of my users to create n number of each record type (accounts, contacts, etc) and associate user defined data with each record. So for example, one of my users might want to associate an SSN with each of his contacts, so I would need to store that additional field for each Contact record he creates.
Thanks!
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要使用外键怎么办?
此代码(未经测试且用于演示)假设存在一组系统范围的自定义字段。为了使其特定于用户,您需要将“user = models.ForiegnKey(User)”添加到类 CustomField 中。
What if you were to use a ForeignKey?
This code (untested and for demo) is assuming there is a system-wide set of custom fields. To make it user-specific, you'd add a "user = models.ForiegnKey(User)" onto the class CustomField.