问题描述
我有几个 Customer
预订 Appointment
.每个 Appointment
都只有一位客户,但可以为一位客户预订在不同时间发生的多个约会.
I have several Customer
s who book Appointment
s. Each Appointment
has exactly one customer, though a customer can be booked for multiple appointments occurring at different times.
class Customer(model.Model):
def __unicode__(self):
return u'%s' % (self.name,)
name = models.CharField(max_length=30)
# and about ten other fields I'd like to see from the admin view.
class Appointment(models.Model):
datetime = models.DateTimeField()
customer = models.ForeignKey("Customer")
class Meta:
ordering = ('datetime',)
现在,当管理员通过查看管理员中的约会(按时间排序)来浏览日程时,有时他们希望查看有关具有特定约会的客户的信息.现在,他们必须记住客户的姓名,从约会导航到客户管理页面,找到记住的客户,然后才能浏览他们的信息.
Now when an admin goes to browse through the schedule by looking at the Appointments (ordered by time) in the admin, sometimes they want to see information about the customer who has a certain appointment. Right now, they'd have to remember the customer's name, navigate from the Appointment to the Customer admin page, find the remembered Customer, and only then could browse their information.
理想情况下,像管理员内联这样的东西会很棒.但是,如果 Customer
有一个 ForeignKey("Appointment")Appointment
管理页面上创建一个 CustomerInline
/代码>.(Django 专门给了我一个错误,说 Customer
has no ForeignKey to Appointment
).有谁知道类似的功能,但是当 Appointment
有 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
)
要将其添加到更改表单中,请参阅:在 Django 管理员的 change_form 中的两个模型字段之间添加自定义 html
And to add this to the change form, see: Add custom html between two model fields in Django admin's change_form
这篇关于Django Admin 的内联解决方案,其中 Admin 包含其他模型的 ForeignKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!