Jantar88
Jantar88

Reputation: 164

How to implement dictionary-like field in Django model?

The problem: a group of several people can use instruments of different types, but the instruments of the same type are not distinguishable. For instance, a group could use 3 hammers and 4 axes, but we don't need to distinguish individual axes or hammers. UPD: One day we could use another instrument type we can't think of now.

How to design a Django model in this case?

My thoughts:

class Person(models.Model):
    name = models.CharField()

class Group(models.Model):
    title = models.CharField()
    members = models.ForeignKey(Person)
    equip = ???

I've found this question on StackExchange and a code snippet for a Django Dictionary model, but I am still not sure what approach to choose. Even more, there are django-jsonfield and django-picklefield suitable for dictionaries.

Also, do I really need a dictionary here?

Upvotes: 1

Views: 623

Answers (1)

Dmitry Shilyaev
Dmitry Shilyaev

Reputation: 733

You have persons, that are related to groups. You assign group of persons to specific Job and track equipment by job, equipment type and its count.

class Group(models.Model):
    title = models.CharField()

class Person(models.Model):
    name = models.CharField()
    group = models.ForeignKey(Group)

class Job(models.Model)
    name = models.CharField()
    group = models.ForeignKey(Group)

class EquipmentUsage(models.Model)
    job = models.ForeignKey(Job)
    equip_name = models.CharField()
    count = models.IntegerField()

Upvotes: 2

Related Questions