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;








0















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.










share|improve this question
























  • 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

















0















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.










share|improve this question
























  • 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













0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















0















"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)





share|improve this answer























  • 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











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
);



);













draft saved

draft discarded


















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









0















"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)





share|improve this answer























  • 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















0















"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)





share|improve this answer























  • 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













0












0








0








"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)





share|improve this answer














"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)






share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴