0

I'm creating a Django model, where (as a simplified example) a user group (Users are all assigned to a group) will be able to access files of a certain file type, and which types a group can access will change by group. The list of files can be any of ~10 common file types. This list might change, but that's probably not common.

Roughly speaking, I see two ways of doing this. Representing the file types as a list of strings for each user [".jpg", ".txt", ".zip"] or, as a M:M relationship with some sort of "FileExtension" Object.

These are cases in which I imagine the ORM route would be much preferred (Ex: If it's important to keep track of Each file extension's use case as for "image", "text", "audio", etc) But what if there's no other information we really need to consider here but the file extension itself? Is there a reason to create a whole model for just a few strings? In my actual project, I want to avoid the complications of creating and maintaining multiple models and storing those in the DB if I can.

Rough Possible Code Examples of each:

String/List approach:

class UserGroup(models.Model):

    group_name = models.CharField(max_length=128)

    file_access = models.JSONField(null=True) #JSON-serialized list of strings

Model approach:

class UserGroup(models.Model):

    group_name = models.CharField(max_length=128)

    file_access = models.ManyToManyField(FileExtension)


class FileExtension(models.Model):

    extension = models.CharField(max_length=8, null=True)

Quick Note: I've already looked into this answer which was helpful in showing me the ways I could do this, but does not leave me convinced as to what the best way is for me to do here. Also the question is a decade old so perhaps some things have changed in how Django handles this.

Brian C
  • 1,333
  • 3
  • 19
  • 36
  • Have a look at [JSONField](https://docs.djangoproject.com/en/3.2/ref/models/fields/#jsonfield), Django >= 3.1 supports this for all supported databases. Also your model approach seems incorrect? As per your implementation a group would only be able to have _one_ extension not many. You instead likely want a [Many-to-many relationship](https://docs.djangoproject.com/en/3.2/topics/db/examples/many_to_many/) – Abdul Aziz Barkat Nov 24 '21 at 17:43
  • You can do it in different ways, but I would prefer second approach either using foreign or m2m relations, I just have one comment on your model field "file_access" which you marked as primary key whereas its not unique because some groups could share some file extensions unless this approach is not permitted from business logic – Husam Alhwadi Nov 24 '21 at 19:44
  • Thanks for catching! Just a stupid typo b/c I copy pasted it from some related code! – Brian C Nov 24 '21 at 19:45
  • Also - Looks like JSONField is a good way to store this if I indeed just want a list of strings - I guess my question now is more the advantages/disadvantages of each approach here. Or which would be most appropriate for my use case? – Brian C Nov 24 '21 at 19:45

0 Answers0