Django管理界面中的Exclusive或ForeignKey
在 Django 中,有没有一种方法可以强制管理员用户选择填充几个外键之一而不是多个?
我有一个类似的模型:
class URL(models.Model): ... links = models.URLField(_('Google Links'),verify_exists=True,unique=True) project = models.ForeignKey(Project,blank=True,null=True) category = models.ForeignKey(Category,blank=True,null=True) person = models.ForeignKey(ExternalPerson,blank=True,null=True) ...
我希望管理员用户选择外键项目、类别或人员之一。 或者我应该以不同的方式组织模型?
In Django is there a way to force admin users to choose to fill one of a few ForeignKeys and not more than one?
I have a model something like :
class URL(models.Model): ... links = models.URLField(_('Google Links'),verify_exists=True,unique=True) project = models.ForeignKey(Project,blank=True,null=True) category = models.ForeignKey(Category,blank=True,null=True) person = models.ForeignKey(ExternalPerson,blank=True,null=True) ...
I want the admin user to choose one of the Foreignkeys project,category or person.
Or should I organize the model differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我所做的是拥有一个指向基本模型的外键,所有其他模型(Google Link、项目等)都从该模型继承。这似乎维持了关系,同时将外键限制为单一选择。
What I did is to have a single foreign key that points to a base model, from which all the other models (Google Link, Projects, etc) inherit. That seems to maintain the relationship while restricting the foreign key to a single choice.
只是一些想法...
我不知道您以后想对这些对象做什么,但是您是否考虑过为所有这些对象创建一个通用接口?它可以解决您的问题...
如果界面不适合您,也许您可以创建一个表单来检查是否仅选择了一个外键。
Just some ideas...
I don't know what you want to do later on with those objects, but have you consider to create a common interface to all of them? It could fix your problem...
If the interface doesn't suit you,maybe you could create a Form to check that just one of the ForeignKeys have been selected.