如何定义模型的字段以从另一个模型的另一个字段中选择另一个应用程序?
我有两个不同的应用程序定义了两个模型。 我想为模型中的特定字段提供一个选择选项,该选项是从另一个应用程序中的模型的另一个字段派生的这些属性。
例如:我有一个模型“ 库存”,该模型具有一个字段 'title' 。
class Inventory(models.Model):
title = models.CharField('Title', max_length=120, default='')
....
我还有另一个模型'发票',它具有字段 'line_one'。这两个模型都位于不同的应用中。
class Invoice(models.Model):
line_one = models.CharField('Line 1', max_length=120)
....
我创建了一个用于添加发票详细信息的Django表单。我想要一个选择 line_one 的选择选项,该选项在库存中存储了所有值。标题字段,以便用户只能从下拉菜单中选择这些值。 我该如何实施? 谢谢。
编辑:我将 line_one 引用到 库存模型的字段,但它似乎有效,但并未完全显示属性但显示“库存对象(7)”,“库存对象(8)”
line_one = models.ForeignKey(Inventory, to_field='title', default=0, on_delete=models.CASCADE)
I have two models defined in two different apps.
I want to have a choice option for a particular field in a model, which is deriving those attributes from another field of a model in another app.
eg: I have a model "Inventory" that has a field 'title'.
class Inventory(models.Model):
title = models.CharField('Title', max_length=120, default='')
....
I also have another model 'Invoice' that has a field 'line_one'. Both these models are located in different apps.
class Invoice(models.Model):
line_one = models.CharField('Line 1', max_length=120)
....
I created a Django form for adding the Invoice details. I want a choice option for line_one that has all the values stored in the Inventory.title field so that the user can only select those values from a dropdown menu.
How can I implement this?
Thanks.
Edit: I referenced line_one to the field title of the Inventory model and it seems to work but it isn't exactly showing the name of the attributes but showing, "Inventory Object(7)", "Inventory Object(8)"
line_one = models.ForeignKey(Inventory, to_field='title', default=0, on_delete=models.CASCADE)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
charfield
line_one
使用
foreferkey
forline_one
(如果您想使用
forefer> foreferykey
)代码>,有必要具有inting.title
的唯一值。Use a
CharField
forline_one
Use a
ForeignKey
forline_one
If you like to use a
ForeignKey
, it is necessary to have unique values forInventory.title
.