简体   繁体   English

Django动态模型参数

[英]Django dynamic model parameters

I have a Job model with fields name, description and some parameters: 我有一个Job模型,其中包含字段名称,描述和一些参数:

class Job(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField(default='Add comprehensive job description')
    # parametes
    lat_lim=[MinValueValidator(-90), MaxValueValidator(90)]
    long_lim=[MinValueValidator(-180), MaxValueValidator(180)]
    lattitude = models.IntegerField('Lattitude North default value', null=True, validators=lat_lim)
    longitude = models.IntegerField('Lattitude South default value', null=True, validators=long_lim)
    time_query_begin = models.DateField('Job query start time', null=True, blank=True)
    ...

Now, I would like a more dynamic approach, where each job has different number of parameters and parameter types. 现在,我想采用一种更动态的方法,其中每个作业具有不同数量的参数和参数类型。 It could be, that job1 has parameters such as height , longitude , lattitude and time_query_begin , and job2 has parameters such as height and temperature etc. 这可能是,该JOB1具有参数如高度经度 ,lattitudetime_query_begin,和JOB2具有参数如高度温度等等。

I was thinking of making a ManyToMany relation to a Parameter model, containing fields name, default_value, min, max. 我正在考虑与参数模型建立ManyToMany关系,其中包含字段名称,default_value,min,max。

What is the best way to accomplish this? 做到这一点的最佳方法是什么?

Note: I have seen this question: Django dynamic model fields , but I not sure if it solves my problem. 注意:我已经看到了这个问题: Django动态模型字段 ,但是我不确定它是否可以解决我的问题。

You could use Django's generic relations scheme, depending on your requirement. 您可以根据需要使用Django的通用关系方案。

However if you have the luxury of postgres 9.4+ (and django 1.9a if you want to use the ORM) the JSONField could be a very attractive way to deal with user defined properties. 但是,如果您使用的是Postgres 9.4+(如果您想使用ORM,则使用django 1.9a),那么JSONField可能是处理用户定义属性的一种非常有吸引力的方法。

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

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