django admin - 如何覆盖默认的

发布于 2024-08-19 05:35:46 字数 627 浏览 6 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2024-08-26 05:35:46

您可以为该位置放置一个内联表,并使用 queryset 方法来处理字段值。
所以,这就是解决方案的想法:

class LocationInline(admin.StackedInline):
    model = Location

    def queryset(self, request):
        query = ... #your custom query here
        qs = super(LocationInline, self).queryset(request).filter(query)
        return qs


class ClientAdmin(admin.ModelAdmin):
    inlines = [LocationInline]
    fieldsets = [
        ('Client Details', {'fields': ['name']}),
    ]

编辑:
在 django 1.1 中还有另一种方法。

class ClientAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Client Details', {'fields': ['name']}),
        ('Location Details', {'fields': ['location']}),
    ]

def formfield_for_foreignkey(self, db_field, request, **kwargs):

    if db_field.name = "location":
        locations = Location.objects.filter( ... ) #your query

        kwargs["queryset"] = Location.objects.filter(query)

    return super(ClientAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

文档在这里: 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:

class LocationInline(admin.StackedInline):
    model = Location

    def queryset(self, request):
        query = ... #your custom query here
        qs = super(LocationInline, self).queryset(request).filter(query)
        return qs


class ClientAdmin(admin.ModelAdmin):
    inlines = [LocationInline]
    fieldsets = [
        ('Client Details', {'fields': ['name']}),
    ]

Edit:
There is another way to do it in django 1.1.

class ClientAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Client Details', {'fields': ['name']}),
        ('Location Details', {'fields': ['location']}),
    ]

def formfield_for_foreignkey(self, db_field, request, **kwargs):

    if db_field.name = "location":
        locations = Location.objects.filter( ... ) #your query

        kwargs["queryset"] = Location.objects.filter(query)

    return super(ClientAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

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! ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文