Honnisha
Honnisha

Reputation: 53

Nested dict in Django model

I have Django project and I need to create model with fields, which every of them must have 17 fields of string, something like nested dict.

Code below doesnt work, just example. I need something like that:

class LimitValues(models.Model):
    stateDescription = models.TextField() # x17


class VSRGraduation(models.Model):
    some_field_1= models.ForeignKey(LimitValues)
    some_field_2= models.ForeignKey(LimitValues)
    some_field_3= models.ForeignKey(LimitValues)
    # etc... total 15 some_fields

Maybe you are faced with same problem.

Upvotes: 1

Views: 371

Answers (1)

levi
levi

Reputation: 22697

You just need to represent it using 1 to Many relationships.

1 VSRGraduation instance can be pointed by Many LimitValues instances

1 LimitValues instance can be pointed by Many LimitValuesState instances

class LimitValuesState(models.Model):
   stateDescription = models.TextField()
   limitvalue = models.ForeignKey(LimitValues)

class LimitValues(models.Model):
    vsr_graduation = models.ForeignKey(VSRGraduation)


class VSRGraduation(models.Model):
     #extra model fields

Upvotes: 2

Related Questions