简体   繁体   English

动态模型的主键

[英]Primary key on dynamic model

I'm building an app with dynamic models (following guidelines on [1]) to create an interface for an already existing database. 我正在使用动态模型(遵循[1]的准则)构建应用程序,以为现有数据库创建接口。

This is my function to create one of the models (in fact it has more fields and stuff, but this should be enough): 这是我创建模型之一的功能(实际上,它具有更多的字段和内容,但这应该足够了):

def create_model(name):
    # Meta class
    class Meta:
        pass

    # Set up a dictionary to simulate declarations within a class
    attrs = {'__module__': 'mymodule',
             'Meta': Meta,
             'fields': {
                'myID': models.CharField(max_length=2, db_column='myID', primary_key=True),
                '__unicode__': lambda self: self.myID,
                }
            }

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

    admin.site.register(model, admin.ModelAdmin)

    return model

But when I try to access this model I get the following OperationalError : 但是,当我尝试访问此模型时,出现以下OperationalError

(1054, "Unknown column 'database_table.id' in 'field list'")

It seems that Django is not taking into account my prymary_key definition for field myID and it's creating the default id field. 看来,Django是没有考虑到我的prymary_key定义字段myID ,它的创建默认的id字段。

Am I missing something? 我想念什么吗? Is it a ''bug'' related to dynamic model creation? 它是与动态模型创建相关的“错误”吗? Anyone knows a workaround for this? 有人知道解决方法吗? Thanks 谢谢

[1] https://code.djangoproject.com/wiki/DynamicModels [1] https://code.djangoproject.com/wiki/DynamicModels

Your fields and methods shouldn't be within the 'fields' dictionary in your attrs , but should be at the top-level of the attrs dict. 您的字段和方法不应位于attrs'fields'字典中,而应位于attrs字典的顶层。

This: 这个:

attrs = {'__module__': 'mymodule',
         'Meta': Meta,
         'fields': {
            'myID': models.CharField(max_length=2, db_column='myID', primary_key=True),
            '__unicode__': lambda self: self.myID,
            }
        }

is functionally equivalent to this: 在功能上等效于此:

class MyModel(models.Model):
    class Meta:
        pass

    fields = { 
       'myID': models.CharField(max_length=2, db_column='myID', primary_key=True),
       '__unicode__': lambda self: self.myID,
      }

    def __unicode__(self):
        return self.myID

The ModelBase metaclass takes any field (actually any object with a contribute_to_class method) that's defined on the top-level of the class, and uses the contribute_to_class method on the field to 'add' it to the actual class. ModelBase元类接受在类的顶层定义的任何字段(实际上是任何使用contribute_to_class方法的对象),并在该字段上使用contribute_to_class方法将其“添加”到实际的类中。 In this process, the field gets added to the fields dictionary, but you shouldn't define the fields dictionary before the actual metaclass is run. 在此过程中,字段将添加到fields字典中,但是在运行实际的元类之前,您不应定义字段字典。 Your attrs should look like this instead: 您的attrs应该看起来像这样:

attrs = {'__module__': 'mymodule',
         'Meta': Meta,
         'myID': models.CharField(max_length=2, db_column='myID', primary_key=True),
         '__unicode__': lambda self: self.myID,
        }

I'm building an app with dynamic models (following guidelines on [1]) to create an interface for an already existing database. 我正在使用动态模型(遵循[1]的准则)构建应用程序,以为现有数据库创建接口。

Are you sure this is the correct approach? 您确定这是正确的方法吗? Don't you want to use manage.py inspectdb to create models from that database instead? 您是否不想使用manage.py inspectdb从该数据库创建模型? See: 看到:

https://docs.djangoproject.com/en/dev/howto/legacy-databases/ https://docs.djangoproject.com/en/dev/howto/legacy-databases/

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

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