1

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.

Community
  • 1
  • 1

1 Answers1

0

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.

Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • Thank you for your answer. I'm using Django 1.8 and PostgreSQL 9.3. I looked into generic relations. It could be the answer. But how can I make the value field of my parameter model generic, so that it can cover both integer and DateTime fields? – Peter Dalgaard Hansen Nov 02 '15 at 14:26