<commit_before>.. _assign:

Assign object permissions
=========================

Assigning object permissions should be very simple once permissions are created
for models.

Prepare permissions
-------------------

Let's assume we have following model:

.. code-block:: python

    class Task(models.Model):
        summary = models.CharField(max_length=32)
        content = models.TextField()
        reported_by = models.ForeignKey(User)
        created_at = models.DateTimeField(auto_now_add=True)

... and we want to be able to set custom permission *view_task*. We let Django
know to do so by adding ``permissions`` tuple to ``Meta`` class and our final
model could look like:

.. code-block:: python

    class Task(models.Model):
        summary = models.CharField(max_length=32)
        content = models.TextField()
        reported_by = models.ForeignKey(User)
        created_at = models.DateTimeField(auto_now_add=True)

        class Meta:
            permissions = (
                ('view_task', 'View task'),
            )

After we call ``syncdb`` management command our *view_task* permission would be
added to default set of permissions.

.. note::
   By default, Django adds 3 permissions for each registered model:

   - *add_modelname*
   - *change_modelname*
   - *delete_modelname*

   (where *modelname* is a simplified name of our model's class). See
   http://docs.djangoproject.com/en/1.2/topics/auth/#default-permissions for
   more detail.

There is nothing new here since creation of permissions is <commit_msg>Extended doc<commit_after>.. _assign:

Assign object permissions
=========================

Assigning object permissions should be very simple once permissions are created
for models.

Prepare permissions
-------------------

Let's assume we have following model:

.. code-block:: python

    class Task(models.Model):
        summary = models.CharField(max_length=32)
        content = models.TextField()
        reported_by = models.ForeignKey(User)
        created_at = models.DateTimeField(auto_now_add=True)

... and we want to be able to set custom permission *view_task*. We let Django
know to do so by adding ``permissions`` tuple to ``Meta`` class and our final
model could look like:

.. code-block:: python

    class Task(models.Model):
        summary = models.CharField(max_length=32)
        content = models.TextField()
        reported_by = models.ForeignKey(User)
        created_at = models.DateTimeField(auto_now_add=True)

        class Meta:
            permissions = (
                ('view_task', 'View task'),
            )

After we call ``syncdb`` (with a ``--all`` switch if you are using south)
management command our *view_task* permission would be added to default set of
permissions.

.. note::
   By default, Django adds 3 permissions for each registered model:

   - *add_modelname*
   - *change_modelname*
   - *delete_modelname*

   (where *modelname* is a simplified name of our model's class). See
   http://docs.djangoproject.com/en/1.2/topics/auth/#default-permissions for
   more detail.

There is nothing new here since creation of permissions is 
{'fraction_non_alphanumeric': 0.11494252873563218, 'fraction_numerical': 0.004469987228607918, 'mean_word_length': 3.0425806451612902, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5363199', 'n_tokens_mistral': 872, 'n_tokens_neox': 838, 'n_words': 298}