<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def add_pii_permissions(self, group, view_only=None):
 """Adds PII model permissions. """

        pii_model_names = [m.split(".")[1] for m in self.pii_models]
        if view_only:
            permissions = Permission.objects.filter(
                (Q(codename__startswith="view") | Q(codename__startswith="display")),
                content_type__model__in=pii_model_names,
            )
        else:
            permissions = Permission.objects.filter(
                content_type__model__in=pii_model_names
            )
        for permission in permissions:
            group.permissions.add(permission)

        for model in self.pii_models:
            permissions = Permission.objects.filter(
                codename__startswith="view",
                content_type__app_label=model.split(".")[0],
                content_type__model=f"historical{model.split('.')[1]}",
            )
            for permission in permissions:
                group.permissions.add(permission)

        for permission in Permission.objects.filter(
            content_type__app_label="edc_registration",
            codename__in=[
                "add_registeredsubject",
                "delete_registeredsubject",
                "change_registeredsubject",
            ],
        ):
            group.permissions.remove(permission)
        permission = Permission.objects.get(
            content_type__app_label="edc_registration",
            codename="view_historicalregisteredsubject",
        )
        group.permissions.add(permission)