dr *_*bob 16 python django foreign-keys django-admin
我有几个Customer人预订了Appointment.每个Appointment客户只有一个客户,但可以预订客户在不同时间进行多次约会.
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',)
Run Code Online (Sandbox Code Playgroud)
现在,当管理员通过查看管理员中的约会(按时间排序)浏览计划时,有时他们希望查看有关具有特定约会的客户的信息.现在,他们必须记住客户的名字,从约会导航到客户管理页面,找到记住的客户,然后才能浏览他们的信息.
理想情况下,管理员内联将是伟大的.但是,如果有一个,我似乎只能CustomerInline在Appointment管理页面Customer上创建一个ForeignKey("Appointment").(Django特别给我一个错误,说Customer没有ForeignKey Appointment).有没有人知道类似的功能,但什么时候Appointment有ForeignKey('Customer')?
注意:我简化了模型; 实际的客户字段目前除了名称(一些自由文本)之外还有大约10个字段,因此将所有信息放入其中是不切实际的__unicode__.
使用django没有简单的方法.内联旨在追随关系.
可能最好的替代方法是提供用户对象的链接.在列表视图中,这非常简单:
在约会模型中添加一个方法,如:
def customer_admin_link(self):
return '<a href="%s">Customer</a>' % reverse('admin:app_label_customer_change %s') % self.id
customer_admin_link.allow_tags = True
customer_admin_link.short_description = 'Customer'
Run Code Online (Sandbox Code Playgroud)
然后在您的ModelAdmin中添加:
list_display = (..., 'customer_admin_link', ...)
Run Code Online (Sandbox Code Playgroud)
另一个以更复杂的方式获得您正在寻找的解决方案的解决方案是定义自定义管理模板.如果你这样做,你基本上可以做任何事情.这是我以前用过的一本指南来解释:http: //www.unessa.net/en/hoyci/2006/12/custom-admin-templates/
基本上从django源复制更改表单并添加代码以显示客户信息.
从上面完成@John的回答- 定义您希望在更改列表中看到的内容:
return '<a href="%s">%s</a>' % (
reverse('admin:applabel_customer_change', (self.customer.id,)),
self.customer.name # add more stuff here
)
Run Code Online (Sandbox Code Playgroud)
要将其添加到更改表单,请参阅:在 Django 管理的 Change_form 中的两个模型字段之间添加自定义 html
| 归档时间: |
|
| 查看次数: |
9367 次 |
| 最近记录: |