简体   繁体   English

遍历外键并将远程模型数据拉入Django admin

[英]Traverse foreign key and pull remote model data into Django admin

Is is possible in the admin to pull a field from a remote model, if you have a local foreign key pointing to that model? 如果您有一个指向该模型的本地外键,管理员是否可以从远程模型中提取字段?

class FirstModel(models.Model):
    [...]
    value12 = models.CharField()

class SecondModel(models.Model):
    [...]
    firstmodel = models.ForeignKey(FirstModel)

And in the Admin I want to pull in value12, any time someone views/edits SecondModel. 并且在管理员中我想要在任何时候有人查看/编辑SecondModel时提取值12。 I figure I can do this through Inlines, but then I lose Fields and FieldSets ordering. 我想我可以通过Inlines来做到这一点,但后来我失去了Fields和FieldSets的订单。 Any other options? 还有其他选择吗? Ideal results would be sortable with fields/fieldsets, -and- read-only. 理想的结果可以使用字段/字段集进行排序,并且只读。

You should be able to access any field in the first model as: firstmodel__value12 您应该能够访问第一个模型中的任何字段:firstmodel__value12

For the list view for the SecondModel: 对于SecondModel的列表视图:

list_display = ('firstmodel__value12',)

For the edit view you can use formfield_overrides . 对于编辑视图,您可以使用formfield_overrides To make it non-editable you specify a read-only widget, eg like this one or provide your own. 要使其不可编辑,请指定一个只读小部件,例如像这个或提供您自己的小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM