用于Django管理员的Inline-like解决方案,其中Admin包含ForeignKey到其他模型 [英] Inline-like solution for Django Admin where Admin contains ForeignKey to other model

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

问题描述

我有几个客户 s谁预订预约 s。每个约会只有一个客户,虽然客户可以预订多个约会发生在不同的时间。

I have several Customers who book Appointments. 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.

理想情况下,像内部管理员一样会很棒。但是,如果客户 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的 answer 从上面 - 定义您想在更改列表中看到的内容:

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
             )

并将其添加到更改表单中,请参阅:在两个模型字段之间添加自定义html n Django admin的change_form

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到其他模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆