I can’t prescribe permissions. I have Accounts, Sites and Informations models:
Accounts
class Accounts(models.Model):
user = models.ForeignKey(to=User, on_delete=models.SET_NULL, null=True, editable=True, blank=True, verbose_name='Пользователь')
name = models.CharField(max_length=255, unique=True, verbose_name='Название')
def __str__(self):
return self.name
Sites and Informations
class Sites(models.Model):
account = models.ForeignKey(to=Accounts, on_delete=models.RESTRICT)
name = models.CharField(max_length=255)
def __str__(self):
return self.name
class Information(models.Model):
site = models.ForeignKey(to=Sites, related_name='information', on_delete=models.RESTRICT)
name_of_data = models.CharField(max_length=255)
data = models.CharField(max_length=255)
def __str__(self):
return self.name_of_data
My view should return a list of sites and information for each site, depending on which account is requested (like /accounts/3 should return all sites and information that belong to the 3rd account). Here are my serializers for more understanding:
from .models import Information, Sites
from app.serializers import AccountsSerializer
class InformationSerializer(serializers.ModelSerializer):
class Meta:
model = Information
fields = ['name_of_data', 'data']
class SitesSerializer(serializers.ModelSerializer):
information = InformationSerializer(many=True, read_only=True)
account = AccountsSerializer(read_only=True)
class Meta:
model = Sites
fields = ['account', 'name', 'information']
I need to restrict user access using the following rule:
- The user who is the owner of the requested account can view information about the account.
But I always get errors and always different errors.
In the above code Permissions is not checked at all and does not work.
kurōzuappu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.