无法创建,迁移模型,将模型注册到管理员并以编程方式重新启动django服务器 [英] unable to create, migrate model, register the model to admin and restart django server programmatically

查看:64
本文介绍了无法创建,迁移模型,将模型注册到管理员并以编程方式重新启动django服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建动态模型,并尝试以编程方式迁移它,并将模型注册到管理站点。我无法破解迁移API,但能够运行管理(makemigrations和migration命令)。我能够做到。但是,当我在管理员中注册模型时,该模型显示为灰色,我必须重新启动服务器才能启用它。我无法以编程方式重新启动服务器。我正在使用Django 3.0.3。

I am trying to create dynamic models and trying to migrate it programmatically and registering a model to the admin site. I could not hack into migration api but was able to run the manage (makemigrations and migrate command). I am able to do it. But when I register the model in the admin, it shows grayed out and I have to restart the server to get it enabled. I have not been able to restart server programmatically. I am using django 3.0.3. Any help here?

model = type(m.get("name"), (models.Model,), m.get("attrs"))

我正在以编程方式启用此处列出的模型中的模型

I am programmatically enabling the models in models list here

def migrate():
    from django.core import management
    from django.db.migrations.recorder import MigrationRecorder

    db_name = "programaticfetchfromdatabase"
    app_label = "programaticfetchfromdatabase"
    try:
        management.call_command("startapp", app_label)
        management.call_command("makemigrations", app_label)
        management.call_command("migrate", app_label, MigrationRecorder.Migration.objects.filter(
                app=app_label).last().__dict__.get("name"), "--database=="+db_name)

        return True
    except:
        # raise Exception("Unable to perform migration)
        return False

if migrate():
    from django.contrib import admin
    # If register and dont restart the model is grayed out
    admin.site.register(model)
   
    # so restarting server here
    # restart server (WHICH API do I use here)
    
    from django.utils import autoreload
    autoreload.restart_with_reloader()

    # I get port already in use error


我想知道为什么动态管理员注册后,del变灰了,或者我想以编程方式重新加载django服务器

I want to know why the model is grayed out after dynamic admin registration or I want to reload the django server programmatically

推荐答案

"--databe="+db_name

"--database=="+db_name

它是

try:
    ...
except:
    ...

不是

try:
    ...
catch:
    ...

最重要的是,您不想使用django runserver在生产网站上运行服务器。

And the most important part, you do not want to use the django runserver to run your server on production websites.

也许可以从django-mutant那里获得灵感,但是我仍然不建议这样做动态模型。

Maybe look at django-mutant for inspiration on what you are trying to do, but I still wouldn't suggest dynamic models.

这篇关于无法创建,迁移模型,将模型注册到管理员并以编程方式重新启动django服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆