2017-04-06 20 views
1

私はdjango 1.11を使用していますが、このリンクhttps://code.djangoproject.com/wiki/DynamicModelsを参照してDjangoの動的モデルを作成しようとしましたが、問題なく実行される各ステップを実行しますが、 djangoの管理パネルで?Django動的モデルの管理者での登録

コンソール

from django.db import models 
from django.contrib import admin 


def create_model(name, fields=None, app_label='', module='', options=None, admin_opts=None): 
    """ 
    Create specified model 
    """ 
    class Meta: 
     # Using type('Meta', ...) gives a dictproxy error during model creation 
     pass 

    if app_label: 
     # app_label must be set using the Meta inner class 
     setattr(Meta, 'app_label', app_label) 

    # Update Meta with any options that were provided 
    if options is not None: 
     for key, value in options.iteritems(): 
      setattr(Meta, key, value) 

    # Set up a dictionary to simulate declarations within a class 
    attrs = {'__module__': module, 'Meta': Meta} 

    # Add in any fields that were provided 
    if fields: 
     attrs.update(fields) 

    # Create the class, which automatically triggers ModelBase processing 
    model = type(name, (models.Model,), attrs) 

    # Create an Admin class if admin options were provided 
    if admin_opts is not None: 
     print admin_opts 
     class Admin(admin.ModelAdmin): 
      pass 
     for key, value in admin_opts: 
      setattr(Admin, key, value) 
     admin.site.register(model, Admin) 

    return model 

action.py:

from action import create_model 
from django.db import models 

fields = { 
    'first_name': models.CharField(max_length=255), 
    'last_name': models.CharField(max_length=255), 
    '__str__': lambda self: '%s %s' (self.first_name, self.last_name), 
} 

options = { 
    'ordering': ['last_name', 'first_name'], 
    'verbose_name': 'valued customer', 
} 

admin_opts = {} 

model = create_model('Person', fields, 
    options=options, 
    admin_opts=admin_opts, 
    app_label='form', 
    module='project.app.model', 
) 

ノー見ることができます。

len(model._meta.fields) 

によってフィールドのしかし、私はどのように管理者で作成したモデルを登録するには、どのようなパラメータが内部admin_opts = {}、私はmakemigrations移行を行うことができ、どのように私はアクセスすることができますどのように来るの見当がつかないこのモデルはviews.pyここから私はこのモデルをインポートします。皆さんが私に助けてください、それは私にとって非常に役に立ち、事前に感謝します。

答えて

1

私はあなたがこの機能を実行するのを忘れたと思います。

def install(model): 
from django.core.management import sql, color 
from django.db import connection 

# Standard syncdb expects models to be in reliable locations, 
# so dynamic models need to bypass django.core.management.syncdb. 
# On the plus side, this allows individual models to be installed 
# without installing the entire project structure. 
# On the other hand, this means that things like relationships and 
# indexes will have to be handled manually. 
# This installs only the basic table definition. 

# disable terminal colors in the sql statements 
style = color.no_style() 

cursor = connection.cursor() 
statements, pending = sql.sql_model_create(model, style) 
for sql in statements: 
    cursor.execute(sql) 
+0

お返事ありがとうございますが、最新のdjangoバージョン1.11を使用していますが、これは 'sql.sql_model_create'が見つかりませんでした。どのようにして1.11でこれを達成できますか? –

+0

Anyどのようにテーブルを作成しましたが、どうすれば管理者に登録できますか?私は多くの方法を疲れたが、私はこれを達成できない。皆さんは私のために私を助けてください。私はとても偉大なものになるでしょう。ありがとうアドバンテンス –

+0

あなたは、この "admin.site.register(model_name)"を試しましたか?views.pyの動的モデルインスタンスを持っていますか? –

3
with connection.schema_editor() as editor: 
     editor.create_model(Model) 

これはgithubのソースコードからのもので、sql_model_createするのではなく、それを試してみて、私は私のプロジェクトの成功にしようと、それは本当だ..

私ので、私は長い時間のために懸命に働いてきました"django 1.10"でdjango-dynamic-modelが見つかりません。

関連する問題