I am building a Django application that exposes a REST API by which users can query 2 of my application's models. I'm following the instructions here .
My two models are:
The model shown below.
class Profile(models.Model): user = models.OneToOneField(User)
My Serialiazers are as follows:
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', )
class ProfileSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Profile
fields = ('id', 'slug', 'user',)
I can see that this is working when I hit the APIs from the command-line:
% curl -H 'Accept: application/json; indent=4' -u root:MyPassword http://127.0.0.1:3001/api/profiles/60/
{
"id": 60,
"slug": "myprofile",
"user": "http://127.0.0.1:3001/api/users/16/"
}
% curl -H 'Accept: application/json; indent=4' -uroot:MyPassword http://127.0.0.1:3001/api/users/16/
{
"url": "http://127.0.0.1:3001/api/users/16/",
"username": "myUser",
"email": "myemail@gmail.com"
}
What I would like to know are two things:
Question #1 Answered correctly here: How to include in queryset details fields of a foreign key (django and rest_api)
Question #2 Re-asked more clearly here: How are permissions to access to Django REST API managed?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.