django admin - 如何覆盖默认的
我有 2 个 django 模型
class Location
address = models.CharField(max_length=100)
city = models.CharField(max_length=20)
class Client
location = models.ForeignKey(Location)
name = models.CharField(max_length=100)
,并且有以下 admin.py
class ClientAdmin(admin.ModelAdmin):
fieldsets = [
('Client Details', {'fields': ['name']}),
('Location Details', {'fields': ['location']}),
]
admin.site.register(Client, ClientAdmin)
当我尝试添加客户端时,我喜欢有一个可编辑的“位置”字段,即地址和城市文本字段。 django 默认情况下给出一个列表..我如何从foregin 表中获取字段?
我有什么办法可以做到这一点?我不想在管理员中注册位置表。
谢谢
I have 2 django model
class Location
address = models.CharField(max_length=100)
city = models.CharField(max_length=20)
class Client
location = models.ForeignKey(Location)
name = models.CharField(max_length=100)
And I have the following admin.py
class ClientAdmin(admin.ModelAdmin):
fieldsets = [
('Client Details', {'fields': ['name']}),
('Location Details', {'fields': ['location']}),
]
admin.site.register(Client, ClientAdmin)
When I try to add a Client I like to have a editable "location" fields i.e. address and city text fields. django by default gives a list.. how can I get the fields from the foregin tables?
any way I can do this? I don't wish to register the Location table in the admin.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为该位置放置一个内联表,并使用 queryset 方法来处理字段值。
所以,这就是解决方案的想法:
编辑:
在 django 1.1 中还有另一种方法。
文档在这里: http:// /docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey
这是一个老问题,但回答这个问题也可以帮助很多其他人。
所以,我希望它有帮助! ;)
You could put an inline table for the location and use the method queryset to handle the field values.
So, this is the ideia of the solution:
Edit:
There is another way to do it in django 1.1.
The docs are here: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey
It's an old question, but answering this can help a lot of other people too.
So, I hope it helps! ;)