简体   繁体   中英

Django dynamic model parameters

I have a Job model with fields name, description and some parameters:

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.

I was thinking of making a ManyToMany relation to a Parameter model, containing fields name, 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.

You could use Django's generic relations scheme, depending on your requirement.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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