简体   繁体   English

Django元数据数据库的最佳实践是什么?

[英]What is the best practice on the metadatabase for Django?

I have to develop a webservice for storing data and I want to do it on Python/Django. 我必须开发一个用于存储数据的Web服务,并且要在Python / Django上进行。 It has only 2 elements at frontend - a table and a diagram. 它的前端只有2个元素-表格和图表。 So basically the output in template is totally standard for all the views in all apps. 因此,基本上,模板中的输出对于所有应用程序中的所有视图都是完全标准的。 I have 2 classes that compile data for output for these 2 elements in JSON, that goes to ReactJS at frontend. 我有2个类为JSON中的这2个元素编译输出数据的数据,这些数据将在前端传递到ReactJS。

Now, I have several apps and the only difference between them are models. 现在,我有几个应用程序,它们之间的唯一区别是模型。 Even CRUD-views and URLS are almost the same for all them. 就连CRUD视图和URL都几乎相同。 So i'm thinking of using some metadatabase to store all data in a unified way. 因此,我正在考虑使用一些元数据库来统一存储所有数据。 Like storing all values in 3 tables - for strings, int and float. 就像将所有值存储在3个表中一样-字符串,整数和浮点数。

The thing that i'm afraid of is performance. 我担心的是性能。 If all my metadata like types of objects, categories and so on will be stored in a single table, I guess it will be hell to get even a simple value out of 4 or 5 requests using Django ORM and a common model for all things in the system. 如果我所有的元数据(如对象类型,类别等)都存储在一个表中,那么我想使用Django ORM从4或5个请求中获得一个简单的值,以及一个通用模型来处理其中的所有事情将是一件令人头疼的事。系统。

On the other hand, there's an option that I can't create new models programmatically on the fly. 另一方面,有一个选项我不能以编程方式即时创建新模型。 Once it's in production - there should be no changes in the code, because of the checksum of the project files. 一旦投入生产,由于项目文件的校验和,代码不应有任何更改。

I need an advice how to handle this situation. 我需要如何处理这种情况的建议。 I thought of something like Drupal metadatabase, but Drupal creates new tables on the fly, and it doesn't need models in the code to work with them. 我想到了类似Drupal元数据库的内容,但是Drupal可以动态创建新表,并且不需要代码中的模型即可使用它们。 Is there a way to do it like that in Django? 有没有办法在Django中做到这一点?

Judging from your requirements you would probably need to use Generic relations: https://docs.djangoproject.com/en/1.8/ref/contrib/contenttypes/ but it wont be the end of the world if all metadata are saved in a single table. 从您的需求来看,您可能需要使用通用关系: https : //docs.djangoproject.com/en/1.8/ref/contrib/contenttypes/,但如果将所有元数据都保存在一个单独的容器中,这将不是世界末日。表。 Lets say you have a 3 different models strings, ints, floats. 假设您有3种不同的模型,字符串,整数,浮点数。 You need to create a MetaData Model that acts as a key value store: 您需要创建一个充当键值存储的MetaData模型:

class MetaData(models.Model):
    key = models.CharField(max_length=256, db_index=True)
    value = models.CharField(max_length=256)

    # Generic relationship to an arbitrary object
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

Then in a simple case you can use save values to this table like that: f=Float.objects.get(pk=1) v = MetaData(content_object=f, key="float_calculation_precision", value="6") v.save() 然后,在一个简单的情况下,您可以像这样使用保存值到该表:f = Float.objects.get(pk = 1)v = MetaData(content_object = f,key =“ float_calculation_precision”,value =“ 6”)v。救()

If you know before hand that you are going to be using only those 3 models you can add a reverse GenericRelation field on each one of those models. 如果您事先知道将只使用这三个模型,则可以在每个模型上添加一个反向GenericRelation字段。

class Floats(models.Model):
    metadata = generic.GenericRelation(MetaData)

class Ints(models.Model):
    metadata = generic.GenericRelation(MetaData)

class Strings(models.Model):
    metadata = generic.GenericRelation(MetaData)

and then you can access their metadata like that: 然后您可以像这样访问他们的元数据:

Floats.metadata.all()

to get all metadata objects or: 获取所有元数据对象,或:

f = Floats.objects.get(pk=1)

f.metadata.all()

to get all metadata objects for a specific object: 获取特定对象的所有元数据对象:

You can also use https://github.com/rafaelsierra/django-metadata package where you can do something like that: 您还可以使用https://github.com/rafaelsierra/django-metadata软件包,在其中可以执行以下操作:

mymodel.metadata['something'] = 'some value'
mymodel.metadata['something']

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

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