本文介绍了用于Django管理员的Inline-like解决方案,其中Admin包含ForeignKey到其他模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 c> c \\ c> cp>管理页面,我似乎只能在 CustomerInline / code>有一个 ForeignKey(Appointment)。 (Django专门给我一个错误,说客户没有ForeignKey到约会)。有人知道类似的功能,但是当约会有一个 ForeignKey('Customer')?

Ideally something like an admin inline would be great. However, I can only seem to make a CustomerInline on the Appointment admin page if Customer had a ForeignKey("Appointment"). (Django specifically gives me an error saying Customer has no ForeignKey to Appointment). Does anyone know of a similar functionality, but when Appointment has a ForeignKey('Customer')?

注意:我简化了模型;实际的客户字段除了名字(一些自由文本)以外还有约10个字段,所以将所有信息放在 __ unicode __ 中是不切实际的。

Note: I simplified the models; the actual Customer field currently has about ~10 fields besides the name (some free text), so it would be impractical to put all the information in the __unicode__.

推荐答案

完成@ John的从上面 - 定义您想在更改列表中看到的内容:

Completing @John's answer from above - define what you would like to see on the your changelist:

return '<a href="%s">%s</a>' % (
                     reverse('admin:applabel_customer_change', (self.customer.id,)),
                     self.customer.name # add more stuff here
             )

并将其添加到更改表单中,请参阅:

And to add this to the change form, see: Add custom html between two model fields in Django admin's change_form

这篇关于用于Django管理员的Inline-like解决方案,其中Admin包含ForeignKey到其他模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:20