如何更改外键字段在 django admin 中的显示方式?

发布于 2024-09-28 09:54:17 字数 348 浏览 3 评论 0原文

我的模型如下所示:

Location:
    state
    county
    city
    street

Building:
    name
    location = ForeignKey(Location)

现在,在管理中,编辑 Building 时,我希望能够以这种方式编辑位置:管理员应该是什么样子

所以,它就像一个内联,但在 Building 中有 Location,而不是相反的方式。

My models look like this:

Location:
    state
    county
    city
    street

Building:
    name
    location = ForeignKey(Location)

Now, in admin, when editing the Building, I want to have the ability to edit the Location in this way:how the admin should look like

So, it's like an inline, but with having Location in Building, not the oposite way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

老街孤人 2024-10-05 09:54:17

如果您在建筑物和位置之间保持一对一的关系,您的问题可能会更容易解决。例如,通过从位置对建筑物进行子类化或将位置字段集成到建筑物中。

我认为没有多少建筑物共享同一位置。因此,无论如何使用位置的外键,您不会节省太多。这个外键也使编辑变得复杂。特别是,如果您想要位置组件的单独输入字段。通常,您首先必须在现有位置中搜索匹配项,然后再创建新的位置条目。

以下示例使 Building 成为 Location 的子类,并将建筑物和位置字段分组到管理表单的两个部分中。不过,您的应用程序可能需要一些微调。

模型:

class Location(models.Model):
    state = models.CharField(max_length=30)
    county = models.CharField(max_length=30)
    city = models.CharField(max_length=30)
    street = models.CharField(max_length=30)

class Building(Location):
    name = models.CharField(max_length=120)

管理表单:

class BuildingAdmin(admin.ModelAdmin):
    fieldsets = (
        ('Building', {
            'fields': ('name',)
        }),
        ('Location', {
            'fields': (('state', 'county', 'city', 'street'),)
        }),
    )

admin.site.register(Building, BuildingAdmin)

Your problem is probably easier to solve if you keep a one-to-one relationship between building and location. For example, by subclassing building from location or by integrating the location fields into buildings.

I assume that not many buildings share the same location. So you would not save much using a foreign key to locations anyway. This foreign key also makes editing complicated. In particular, if you want separate entry fields for location components. Normally, you would first have to search existing locations for a match before creating a new location entry.

The following example makes Building a subclass of Location and groups building and location fields into two sections of the admin form. Your application will probably require some fine tuning though.

The model:

class Location(models.Model):
    state = models.CharField(max_length=30)
    county = models.CharField(max_length=30)
    city = models.CharField(max_length=30)
    street = models.CharField(max_length=30)

class Building(Location):
    name = models.CharField(max_length=120)

The admin form:

class BuildingAdmin(admin.ModelAdmin):
    fieldsets = (
        ('Building', {
            'fields': ('name',)
        }),
        ('Location', {
            'fields': (('state', 'county', 'city', 'street'),)
        }),
    )

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