简体   繁体   English

Django将字段添加到现有模型应用程序

[英]Django add field to existing model app

in my new Django app I use 3rd-part Photologue app. 在我的新Django应用中,我使用了第三部分的Photologue应用。 This app works well, but I need to add some field in its model without change the original source code of Photologue app. 该应用程序运行良好,但我需要在其模型中添加一些字段而不更改Photologue应用程序的原始源代码。

With this snippet, all works well, but maybe this is not the best solution: 使用此代码段,所有代码都可以正常运行,但这也许不是最佳解决方案:

Photo.add_to_class("link", models.URLField(max_leng.... ))

I've read some posts where I should use OneToOneRelationField ... but then, in django admin app, I will have two different form page of Photo and new PhotoLink class... 我已经阅读了一些我应该使用OneToOneRelationField的帖子...但是,在Django管理应用程序中,我将有两个不同的Photo表单页面和新的PhotoLink类...

Does anyone can help me? 有人可以帮助我吗?

If you are using a OneToOneField , you can use your 'extension' model as an inline to the model you are extending. 如果您使用的是OneToOneField ,则可以将“扩展”模型用作扩展模型的内联。 You do this by first un-registering the original admin class, and then overriding it with our variant: 为此,您首先要取消注册原始admin类,然后使用我们的变体覆盖它:

from otherapp.admin import OriginalFooAdmin
from otherapp.models import Foo
from yourapp.models import YourExtension
from django.contrib import admin

admin.site.unregister(Foo)

class ExtensionInline(admin.StackedInline):
    model = YourExtension

class NewFooAdmin(OriginalFooAdmin):
    inlines = OriginalFooAdmin.inlines + [ExtensionInline]
admin.site.register(Foo, NewFooAdmin)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM