how to be authorized to create an object using django rest framework and CreateAPIView?How to send headers in Django rest framework browsable APISaving form data rewrites the same rowLoop inside loop in queryset in Django templateDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to expose some specific fields of model_b based on a field of model_a?Customer User Authentication error : AttributeError: Manager isn't available; 'auth.User' has been swapped for 'user_management.CustomUser'How to set dynamic initial values to django modelform fieldDjango Model Issue In Multilevel Inheritance.How to implement update_or_create inside create method of ModelSerializerHow to check if Django Signal works?How to edit user permission in Django Rest Framework
What to call a small, open stone or cement reservoir that supplies fresh water from a spring or other natural source?
Presenting 2 results for one variable using a left brace
Was murdering a slave illegal in American slavery, and if so, what punishments were given for it?
Schwa-less Polysyllabic German Noun Stems of Germanic Origin
Simple Arithmetic Puzzle 7. Or is it?
Connecting circles clockwise in TikZ
Does science define life as "beginning at conception"?
Bash - Execute two commands and get exit status 1 if first fails
How to become an Editorial board member?
Why is this python script running in background consuming 100 % CPU?
1950s or earlier book with electrical currents living on Pluto
Does a windmilling propeller create more drag than a stopped propeller in an engine out scenario?
If you attack a Tarrasque while swallowed, what AC do you need to beat to hit it?
Is my company merging branches wrong?
Is there a realtime, uncut video of Saturn V ignition through tower clear?
How does the +1 Keen Composite Longbow (+2 Str) work?
How did Jean Parisot de Valette, 49th Grand Master of the Order of Malta, die?
How to safely discharge oneself
Warped chessboard
Is presenting a play showing Military characters in a bad light a crime in the US?
pwaS eht tirsf dna tasl setterl fo hace dorw
Was Tyrion always a poor strategist?
Gambler's Fallacy Dice
Parse a C++14 integer literal
how to be authorized to create an object using django rest framework and CreateAPIView?
How to send headers in Django rest framework browsable APISaving form data rewrites the same rowLoop inside loop in queryset in Django templateDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to expose some specific fields of model_b based on a field of model_a?Customer User Authentication error : AttributeError: Manager isn't available; 'auth.User' has been swapped for 'user_management.CustomUser'How to set dynamic initial values to django modelform fieldDjango Model Issue In Multilevel Inheritance.How to implement update_or_create inside create method of ModelSerializerHow to check if Django Signal works?How to edit user permission in Django Rest Framework
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a problem when I want to create an object using CreateAPIView, I get the message:
"detail": "Authentication credentials were not provided.".
I use rest-auth and rest-authtoken apps.
this is what I made so far:
models.py
class CustomUser(AbstractUser):
objects = CustomUserManager()
is_normal_user = models.BooleanField(default=False)
is_corporate_user = models.BooleanField(default=False)
class CompanyProfile(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
corporate_name = models.CharField(max_length=30)
serializers.py
class CompanyProfileSerializer(serializers.ModelSerializer):
class Meta:
model = CompanyProfile
fields = ['user', 'corporate_name',]
read_only_fields = ('id',)
views.py
class Authorized_Company_User(permissions.BasePermission):
def has_permission(self, request, view):
return bool(request.user and request.user.is_corporate_user)
class CompanyCreateProfileView(generics.CreateAPIView):
#queryset = CompanyProfile.objects.all()
serializer_class = CompanyProfileSerializer
#authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated, Authorized_Company_User)
I am wondering if I need to define create function, and use get method to get user authtoken.
django django-rest-framework django-rest-auth
add a comment |
I have a problem when I want to create an object using CreateAPIView, I get the message:
"detail": "Authentication credentials were not provided.".
I use rest-auth and rest-authtoken apps.
this is what I made so far:
models.py
class CustomUser(AbstractUser):
objects = CustomUserManager()
is_normal_user = models.BooleanField(default=False)
is_corporate_user = models.BooleanField(default=False)
class CompanyProfile(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
corporate_name = models.CharField(max_length=30)
serializers.py
class CompanyProfileSerializer(serializers.ModelSerializer):
class Meta:
model = CompanyProfile
fields = ['user', 'corporate_name',]
read_only_fields = ('id',)
views.py
class Authorized_Company_User(permissions.BasePermission):
def has_permission(self, request, view):
return bool(request.user and request.user.is_corporate_user)
class CompanyCreateProfileView(generics.CreateAPIView):
#queryset = CompanyProfile.objects.all()
serializer_class = CompanyProfileSerializer
#authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated, Authorized_Company_User)
I am wondering if I need to define create function, and use get method to get user authtoken.
django django-rest-framework django-rest-auth
In general, you need to provide some authentication data to the api. It could be provided in a cookie or a specific header. Do you have a mechanism to obtain a token in the client and send it to the api for subsequent requests?
– Ozgur Akcali
Mar 23 at 19:53
yes, I am using header extension on my web browser. should I depend on that to build my web app?
– omeraiman
Mar 24 at 8:08
add a comment |
I have a problem when I want to create an object using CreateAPIView, I get the message:
"detail": "Authentication credentials were not provided.".
I use rest-auth and rest-authtoken apps.
this is what I made so far:
models.py
class CustomUser(AbstractUser):
objects = CustomUserManager()
is_normal_user = models.BooleanField(default=False)
is_corporate_user = models.BooleanField(default=False)
class CompanyProfile(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
corporate_name = models.CharField(max_length=30)
serializers.py
class CompanyProfileSerializer(serializers.ModelSerializer):
class Meta:
model = CompanyProfile
fields = ['user', 'corporate_name',]
read_only_fields = ('id',)
views.py
class Authorized_Company_User(permissions.BasePermission):
def has_permission(self, request, view):
return bool(request.user and request.user.is_corporate_user)
class CompanyCreateProfileView(generics.CreateAPIView):
#queryset = CompanyProfile.objects.all()
serializer_class = CompanyProfileSerializer
#authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated, Authorized_Company_User)
I am wondering if I need to define create function, and use get method to get user authtoken.
django django-rest-framework django-rest-auth
I have a problem when I want to create an object using CreateAPIView, I get the message:
"detail": "Authentication credentials were not provided.".
I use rest-auth and rest-authtoken apps.
this is what I made so far:
models.py
class CustomUser(AbstractUser):
objects = CustomUserManager()
is_normal_user = models.BooleanField(default=False)
is_corporate_user = models.BooleanField(default=False)
class CompanyProfile(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
corporate_name = models.CharField(max_length=30)
serializers.py
class CompanyProfileSerializer(serializers.ModelSerializer):
class Meta:
model = CompanyProfile
fields = ['user', 'corporate_name',]
read_only_fields = ('id',)
views.py
class Authorized_Company_User(permissions.BasePermission):
def has_permission(self, request, view):
return bool(request.user and request.user.is_corporate_user)
class CompanyCreateProfileView(generics.CreateAPIView):
#queryset = CompanyProfile.objects.all()
serializer_class = CompanyProfileSerializer
#authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated, Authorized_Company_User)
I am wondering if I need to define create function, and use get method to get user authtoken.
django django-rest-framework django-rest-auth
django django-rest-framework django-rest-auth
edited Mar 24 at 7:42
Navid2zp
1,790518
1,790518
asked Mar 23 at 19:40
omeraimanomeraiman
257
257
In general, you need to provide some authentication data to the api. It could be provided in a cookie or a specific header. Do you have a mechanism to obtain a token in the client and send it to the api for subsequent requests?
– Ozgur Akcali
Mar 23 at 19:53
yes, I am using header extension on my web browser. should I depend on that to build my web app?
– omeraiman
Mar 24 at 8:08
add a comment |
In general, you need to provide some authentication data to the api. It could be provided in a cookie or a specific header. Do you have a mechanism to obtain a token in the client and send it to the api for subsequent requests?
– Ozgur Akcali
Mar 23 at 19:53
yes, I am using header extension on my web browser. should I depend on that to build my web app?
– omeraiman
Mar 24 at 8:08
In general, you need to provide some authentication data to the api. It could be provided in a cookie or a specific header. Do you have a mechanism to obtain a token in the client and send it to the api for subsequent requests?
– Ozgur Akcali
Mar 23 at 19:53
In general, you need to provide some authentication data to the api. It could be provided in a cookie or a specific header. Do you have a mechanism to obtain a token in the client and send it to the api for subsequent requests?
– Ozgur Akcali
Mar 23 at 19:53
yes, I am using header extension on my web browser. should I depend on that to build my web app?
– omeraiman
Mar 24 at 8:08
yes, I am using header extension on my web browser. should I depend on that to build my web app?
– omeraiman
Mar 24 at 8:08
add a comment |
1 Answer
1
active
oldest
votes
"detail": "Authentication credentials were not provided."
this message is caused by the permission_classes = IsAuthenticated
You need to provide a Token to be able to create.
Add this url from rest_auth app:
re_path(r'^rest_auth/',include('rest_auth.urls'))
then you can use postman to make tests
method :POST
url: http://127.0.0.1:8000/rest_auth/login/
body: "username":"user", "password":"password"
headers: Content-Type: Application/json
as response you get
`"key":"here your token"`
with this token you can add a new user
method :POST
url: http://127.0.0.1:8000/add_user_url/
body: "corporate_name":"corporate"
headers: Content-Type: Application/json
Authorization: "Token ########here your token########"
And in your CreateAPIView you can assign the user:
class CompanyCreateProfileView(generics.CreateAPIView):
def perform_create(self, serializer):
serializer.save(user=self.request.user)
I solved authentication token using the answer in this question: stackoverflow.com/questions/39695187/…
– omeraiman
Mar 24 at 8:48
just remain one problem, I want the user to only create profile for himself, because I am getting all the users to choose from. thanks.
– omeraiman
Mar 24 at 8:50
Yes user can create companyProfile by assigning the authenticated user. serializer.save(user=self.request.user)
– idirall22
Mar 24 at 9:04
thank you for help. I added this line to the serializer: user = serializers.PrimaryKeyRelatedField( read_only=True, default=serializers.CurrentUserDefault() )
– omeraiman
Mar 24 at 12:46
1
and finally, it worked.
– omeraiman
Mar 24 at 12:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317653%2fhow-to-be-authorized-to-create-an-object-using-django-rest-framework-and-createa%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
"detail": "Authentication credentials were not provided."
this message is caused by the permission_classes = IsAuthenticated
You need to provide a Token to be able to create.
Add this url from rest_auth app:
re_path(r'^rest_auth/',include('rest_auth.urls'))
then you can use postman to make tests
method :POST
url: http://127.0.0.1:8000/rest_auth/login/
body: "username":"user", "password":"password"
headers: Content-Type: Application/json
as response you get
`"key":"here your token"`
with this token you can add a new user
method :POST
url: http://127.0.0.1:8000/add_user_url/
body: "corporate_name":"corporate"
headers: Content-Type: Application/json
Authorization: "Token ########here your token########"
And in your CreateAPIView you can assign the user:
class CompanyCreateProfileView(generics.CreateAPIView):
def perform_create(self, serializer):
serializer.save(user=self.request.user)
I solved authentication token using the answer in this question: stackoverflow.com/questions/39695187/…
– omeraiman
Mar 24 at 8:48
just remain one problem, I want the user to only create profile for himself, because I am getting all the users to choose from. thanks.
– omeraiman
Mar 24 at 8:50
Yes user can create companyProfile by assigning the authenticated user. serializer.save(user=self.request.user)
– idirall22
Mar 24 at 9:04
thank you for help. I added this line to the serializer: user = serializers.PrimaryKeyRelatedField( read_only=True, default=serializers.CurrentUserDefault() )
– omeraiman
Mar 24 at 12:46
1
and finally, it worked.
– omeraiman
Mar 24 at 12:47
add a comment |
"detail": "Authentication credentials were not provided."
this message is caused by the permission_classes = IsAuthenticated
You need to provide a Token to be able to create.
Add this url from rest_auth app:
re_path(r'^rest_auth/',include('rest_auth.urls'))
then you can use postman to make tests
method :POST
url: http://127.0.0.1:8000/rest_auth/login/
body: "username":"user", "password":"password"
headers: Content-Type: Application/json
as response you get
`"key":"here your token"`
with this token you can add a new user
method :POST
url: http://127.0.0.1:8000/add_user_url/
body: "corporate_name":"corporate"
headers: Content-Type: Application/json
Authorization: "Token ########here your token########"
And in your CreateAPIView you can assign the user:
class CompanyCreateProfileView(generics.CreateAPIView):
def perform_create(self, serializer):
serializer.save(user=self.request.user)
I solved authentication token using the answer in this question: stackoverflow.com/questions/39695187/…
– omeraiman
Mar 24 at 8:48
just remain one problem, I want the user to only create profile for himself, because I am getting all the users to choose from. thanks.
– omeraiman
Mar 24 at 8:50
Yes user can create companyProfile by assigning the authenticated user. serializer.save(user=self.request.user)
– idirall22
Mar 24 at 9:04
thank you for help. I added this line to the serializer: user = serializers.PrimaryKeyRelatedField( read_only=True, default=serializers.CurrentUserDefault() )
– omeraiman
Mar 24 at 12:46
1
and finally, it worked.
– omeraiman
Mar 24 at 12:47
add a comment |
"detail": "Authentication credentials were not provided."
this message is caused by the permission_classes = IsAuthenticated
You need to provide a Token to be able to create.
Add this url from rest_auth app:
re_path(r'^rest_auth/',include('rest_auth.urls'))
then you can use postman to make tests
method :POST
url: http://127.0.0.1:8000/rest_auth/login/
body: "username":"user", "password":"password"
headers: Content-Type: Application/json
as response you get
`"key":"here your token"`
with this token you can add a new user
method :POST
url: http://127.0.0.1:8000/add_user_url/
body: "corporate_name":"corporate"
headers: Content-Type: Application/json
Authorization: "Token ########here your token########"
And in your CreateAPIView you can assign the user:
class CompanyCreateProfileView(generics.CreateAPIView):
def perform_create(self, serializer):
serializer.save(user=self.request.user)
"detail": "Authentication credentials were not provided."
this message is caused by the permission_classes = IsAuthenticated
You need to provide a Token to be able to create.
Add this url from rest_auth app:
re_path(r'^rest_auth/',include('rest_auth.urls'))
then you can use postman to make tests
method :POST
url: http://127.0.0.1:8000/rest_auth/login/
body: "username":"user", "password":"password"
headers: Content-Type: Application/json
as response you get
`"key":"here your token"`
with this token you can add a new user
method :POST
url: http://127.0.0.1:8000/add_user_url/
body: "corporate_name":"corporate"
headers: Content-Type: Application/json
Authorization: "Token ########here your token########"
And in your CreateAPIView you can assign the user:
class CompanyCreateProfileView(generics.CreateAPIView):
def perform_create(self, serializer):
serializer.save(user=self.request.user)
answered Mar 24 at 8:22
idirall22idirall22
609
609
I solved authentication token using the answer in this question: stackoverflow.com/questions/39695187/…
– omeraiman
Mar 24 at 8:48
just remain one problem, I want the user to only create profile for himself, because I am getting all the users to choose from. thanks.
– omeraiman
Mar 24 at 8:50
Yes user can create companyProfile by assigning the authenticated user. serializer.save(user=self.request.user)
– idirall22
Mar 24 at 9:04
thank you for help. I added this line to the serializer: user = serializers.PrimaryKeyRelatedField( read_only=True, default=serializers.CurrentUserDefault() )
– omeraiman
Mar 24 at 12:46
1
and finally, it worked.
– omeraiman
Mar 24 at 12:47
add a comment |
I solved authentication token using the answer in this question: stackoverflow.com/questions/39695187/…
– omeraiman
Mar 24 at 8:48
just remain one problem, I want the user to only create profile for himself, because I am getting all the users to choose from. thanks.
– omeraiman
Mar 24 at 8:50
Yes user can create companyProfile by assigning the authenticated user. serializer.save(user=self.request.user)
– idirall22
Mar 24 at 9:04
thank you for help. I added this line to the serializer: user = serializers.PrimaryKeyRelatedField( read_only=True, default=serializers.CurrentUserDefault() )
– omeraiman
Mar 24 at 12:46
1
and finally, it worked.
– omeraiman
Mar 24 at 12:47
I solved authentication token using the answer in this question: stackoverflow.com/questions/39695187/…
– omeraiman
Mar 24 at 8:48
I solved authentication token using the answer in this question: stackoverflow.com/questions/39695187/…
– omeraiman
Mar 24 at 8:48
just remain one problem, I want the user to only create profile for himself, because I am getting all the users to choose from. thanks.
– omeraiman
Mar 24 at 8:50
just remain one problem, I want the user to only create profile for himself, because I am getting all the users to choose from. thanks.
– omeraiman
Mar 24 at 8:50
Yes user can create companyProfile by assigning the authenticated user. serializer.save(user=self.request.user)
– idirall22
Mar 24 at 9:04
Yes user can create companyProfile by assigning the authenticated user. serializer.save(user=self.request.user)
– idirall22
Mar 24 at 9:04
thank you for help. I added this line to the serializer: user = serializers.PrimaryKeyRelatedField( read_only=True, default=serializers.CurrentUserDefault() )
– omeraiman
Mar 24 at 12:46
thank you for help. I added this line to the serializer: user = serializers.PrimaryKeyRelatedField( read_only=True, default=serializers.CurrentUserDefault() )
– omeraiman
Mar 24 at 12:46
1
1
and finally, it worked.
– omeraiman
Mar 24 at 12:47
and finally, it worked.
– omeraiman
Mar 24 at 12:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317653%2fhow-to-be-authorized-to-create-an-object-using-django-rest-framework-and-createa%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In general, you need to provide some authentication data to the api. It could be provided in a cookie or a specific header. Do you have a mechanism to obtain a token in the client and send it to the api for subsequent requests?
– Ozgur Akcali
Mar 23 at 19:53
yes, I am using header extension on my web browser. should I depend on that to build my web app?
– omeraiman
Mar 24 at 8:08