0

So I've got two models structed like:

class DataSheetsCluster(models.Model):
    """
    Represents a group of datasheets made by the owner.
    Has it's own icon, name and description.
    A parent can only have one owner at a time.
    """

    class Meta:
        permissions = [
            ('READ_DATA', 'can read cluster data'),
            ('WRITE_DATA', 'can write to cluster'),
            ('MANAGE_CLUSTER', 'can manage cluster'),
            ('MANAGE_FIELD_DATA', "can manage cluster's field data"),
            ('MANAGE_DATASHEETS', "can manage cluster's datasheets"),
            ('MANAGE_ROLES', "can manage cluster's roles"),
            ('MANAGE_FIELDS', "can manage cluster's fields")
        ]

    objects = models.Manager()
    id = models.BigAutoField(primary_key=True, db_index=True, editable=False, auto_created=True)
    name = models.CharField(max_length=50)
    description = models.CharField(max_length=1024, db_index=True)
    members = models.ManyToManyField('api_backend.Member')
    roles = models.ManyToManyField('api_backend.Role')
    datasheets = models.ManyToManyField(DataSheet)
    total_size_limit = models.FloatField(null=True, blank=True, db_index=True)
    created_at = models.DateTimeField(auto_now=True, editable=False, db_index=True)

    REQUIRED_FIELDS = [name]
class DataSheet(models.Model):
    """
    Represents a single dataSheet.
    dataSheets have their own model at the core. Model data is added to
    the dataSheets in the form of separate records.
    """

    class Meta:
        permissions = [
            ('READ_DATA', 'can read datasheet data'),
            ('WRITE_DATA', 'can write to datasheet'),
            ('MANAGE_FIELD_DATA', "can manage datasheet's field data"),
            ('MANAGE_ROLES', "can manage datasheet's roles"),
            ('MANAGE_FIELDS', "can manage datasheet's fields")
        ]

    objects = models.Manager()
    id = models.BigAutoField(primary_key=True, db_index=True, editable=False, auto_created=True)
    name = models.CharField(max_length=75, db_index=True)
    description = models.CharField(max_length=1024, db_index=True)
    owner = models.ForeignKey('api_backend.Member', on_delete=models.CASCADE, db_index=True)
    fields = models.ManyToManyField('api_backend.DataSheetField')
    overwrites = models.ManyToManyField('api_backend.RoleOverwrite')
    parent = models.ForeignKey('api_backend.DataSheetsCategory', on_delete=models.CASCADE, db_index=True, null=True,
                               blank=True)
    cluster = models.ForeignKey('api_backend.DataSheetsCluster', on_delete=models.CASCADE, db_index=True)
    created_at = models.DateTimeField(auto_now=True, editable=False, db_index=True)

    REQUIRED_FIELDS = [name, owner, cluster]

The idea behind them being, they both must have the same common set of permissions [like READ_DATA, WRITE_DATA and MANAGE_ROLES]. However, when I access the READ_DATA permission for a DataSheetsCluster, it refers to something different than the READ_DATA permission for a datasheet,

Like the permissions set for DataSheetsCluster is a global set of permissions, and the other is a local set of the same.

Can this idea be implemented in django?

Aryan Iyappan
  • 323
  • 1
  • 3
  • 15
  • hi, if get the question right, you may create a model which will be used for the permissions refer to it. but those two models you already have, inherit from that model instead so the same permissions would cover them too. – mh-firouzjah Dec 24 '20 at 06:08
  • Yeah, I could do that for the common set of permissions for each model, but then, there would be a few other model-specific permissions for DataSheetsCluster. How can I add that too? – Aryan Iyappan Dec 25 '20 at 09:35
  • you asked for common permissions, and I think that would work. this new question is not related to that problem. write its own permission for itself. – mh-firouzjah Dec 25 '20 at 10:26

0 Answers0