How do I get a value for a non-pk field in a fk?












0















Good evening,



I am relatively knew to Django but I am not understanding how I get a value for a different field referenced by a Foreign Key.



For example, I have a model:



class Status(models.Model):
status_text = models.CharField(max_length=20)
status_open = models.BooleanField(default=1)

def __str__(self):
return self.status_text

class Target(models.Model):
last_name = models.CharField(max_length=40)
first_name = models.CharField(max_length=40)
status = models.ForeignKey(Status, on_delete=models.CASCADE)


I want to query it:



queryset = Target.objects.filter(status__status_open=1)


When I do this, I get returned the pk (1,2,3, etc.) for the status field. What I really want is the status_text that corresponds to that pk.



I have been playing with select related but it just states that status_text is not a FK.



Basically, how do I simulate a query like:



SELECT Target.last_name, Target.first_name, Status.status_text 
FROM Target, Status
WHERE Target.status = Status.ID AND Status.status_open = 1;


Thanks for your help. I continue to struggle with the ORM in my usage of Django...



EDIT: Adding Serializer & views.py



class TargetSerializer(serializers.ModelSerializer):
status_text = serializers.ReadOnlyField()

class Meta:
model = Target
fields = ('last_name', 'first_name', 'status_text')


Views.py



class TargetList(generics.ListAPIView):
queryset = Target.objects.filter(sir_status__status_open=1).values( 'last_name', 'first_name', 'sir_status__status_text')
serializer_class = TargetSerializer


My API call is returning data without errors, but NOT the status_text.



The data in the status table is simply a pk ID (1,2,3) & a string ('Open', 'Closed')



Thanks for your help.










share|improve this question

























  • That original query does not give you the PK of the related model. What makes you think it does? Each item in queryset will have a status attribute which is the related Status item itself.

    – Daniel Roseman
    Jan 1 at 11:00


















0















Good evening,



I am relatively knew to Django but I am not understanding how I get a value for a different field referenced by a Foreign Key.



For example, I have a model:



class Status(models.Model):
status_text = models.CharField(max_length=20)
status_open = models.BooleanField(default=1)

def __str__(self):
return self.status_text

class Target(models.Model):
last_name = models.CharField(max_length=40)
first_name = models.CharField(max_length=40)
status = models.ForeignKey(Status, on_delete=models.CASCADE)


I want to query it:



queryset = Target.objects.filter(status__status_open=1)


When I do this, I get returned the pk (1,2,3, etc.) for the status field. What I really want is the status_text that corresponds to that pk.



I have been playing with select related but it just states that status_text is not a FK.



Basically, how do I simulate a query like:



SELECT Target.last_name, Target.first_name, Status.status_text 
FROM Target, Status
WHERE Target.status = Status.ID AND Status.status_open = 1;


Thanks for your help. I continue to struggle with the ORM in my usage of Django...



EDIT: Adding Serializer & views.py



class TargetSerializer(serializers.ModelSerializer):
status_text = serializers.ReadOnlyField()

class Meta:
model = Target
fields = ('last_name', 'first_name', 'status_text')


Views.py



class TargetList(generics.ListAPIView):
queryset = Target.objects.filter(sir_status__status_open=1).values( 'last_name', 'first_name', 'sir_status__status_text')
serializer_class = TargetSerializer


My API call is returning data without errors, but NOT the status_text.



The data in the status table is simply a pk ID (1,2,3) & a string ('Open', 'Closed')



Thanks for your help.










share|improve this question

























  • That original query does not give you the PK of the related model. What makes you think it does? Each item in queryset will have a status attribute which is the related Status item itself.

    – Daniel Roseman
    Jan 1 at 11:00
















0












0








0








Good evening,



I am relatively knew to Django but I am not understanding how I get a value for a different field referenced by a Foreign Key.



For example, I have a model:



class Status(models.Model):
status_text = models.CharField(max_length=20)
status_open = models.BooleanField(default=1)

def __str__(self):
return self.status_text

class Target(models.Model):
last_name = models.CharField(max_length=40)
first_name = models.CharField(max_length=40)
status = models.ForeignKey(Status, on_delete=models.CASCADE)


I want to query it:



queryset = Target.objects.filter(status__status_open=1)


When I do this, I get returned the pk (1,2,3, etc.) for the status field. What I really want is the status_text that corresponds to that pk.



I have been playing with select related but it just states that status_text is not a FK.



Basically, how do I simulate a query like:



SELECT Target.last_name, Target.first_name, Status.status_text 
FROM Target, Status
WHERE Target.status = Status.ID AND Status.status_open = 1;


Thanks for your help. I continue to struggle with the ORM in my usage of Django...



EDIT: Adding Serializer & views.py



class TargetSerializer(serializers.ModelSerializer):
status_text = serializers.ReadOnlyField()

class Meta:
model = Target
fields = ('last_name', 'first_name', 'status_text')


Views.py



class TargetList(generics.ListAPIView):
queryset = Target.objects.filter(sir_status__status_open=1).values( 'last_name', 'first_name', 'sir_status__status_text')
serializer_class = TargetSerializer


My API call is returning data without errors, but NOT the status_text.



The data in the status table is simply a pk ID (1,2,3) & a string ('Open', 'Closed')



Thanks for your help.










share|improve this question
















Good evening,



I am relatively knew to Django but I am not understanding how I get a value for a different field referenced by a Foreign Key.



For example, I have a model:



class Status(models.Model):
status_text = models.CharField(max_length=20)
status_open = models.BooleanField(default=1)

def __str__(self):
return self.status_text

class Target(models.Model):
last_name = models.CharField(max_length=40)
first_name = models.CharField(max_length=40)
status = models.ForeignKey(Status, on_delete=models.CASCADE)


I want to query it:



queryset = Target.objects.filter(status__status_open=1)


When I do this, I get returned the pk (1,2,3, etc.) for the status field. What I really want is the status_text that corresponds to that pk.



I have been playing with select related but it just states that status_text is not a FK.



Basically, how do I simulate a query like:



SELECT Target.last_name, Target.first_name, Status.status_text 
FROM Target, Status
WHERE Target.status = Status.ID AND Status.status_open = 1;


Thanks for your help. I continue to struggle with the ORM in my usage of Django...



EDIT: Adding Serializer & views.py



class TargetSerializer(serializers.ModelSerializer):
status_text = serializers.ReadOnlyField()

class Meta:
model = Target
fields = ('last_name', 'first_name', 'status_text')


Views.py



class TargetList(generics.ListAPIView):
queryset = Target.objects.filter(sir_status__status_open=1).values( 'last_name', 'first_name', 'sir_status__status_text')
serializer_class = TargetSerializer


My API call is returning data without errors, but NOT the status_text.



The data in the status table is simply a pk ID (1,2,3) & a string ('Open', 'Closed')



Thanks for your help.







django django-rest-framework django-orm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 16:30







Bring Coffee Bring Beer

















asked Jan 1 at 4:31









Bring Coffee Bring BeerBring Coffee Bring Beer

952412




952412













  • That original query does not give you the PK of the related model. What makes you think it does? Each item in queryset will have a status attribute which is the related Status item itself.

    – Daniel Roseman
    Jan 1 at 11:00





















  • That original query does not give you the PK of the related model. What makes you think it does? Each item in queryset will have a status attribute which is the related Status item itself.

    – Daniel Roseman
    Jan 1 at 11:00



















That original query does not give you the PK of the related model. What makes you think it does? Each item in queryset will have a status attribute which is the related Status item itself.

– Daniel Roseman
Jan 1 at 11:00







That original query does not give you the PK of the related model. What makes you think it does? Each item in queryset will have a status attribute which is the related Status item itself.

– Daniel Roseman
Jan 1 at 11:00














1 Answer
1






active

oldest

votes


















0














Use values() method



Target.objects.filter(status__status_open=1).values('last_name', 'first_name', 'status__status_text')





share|improve this answer
























  • Using the django-rest-framework, how do I add that to the serializer?

    – Bring Coffee Bring Beer
    Jan 1 at 5:24











  • pls do add your current configuration of serializer along with current output and required output :)

    – JPG
    Jan 1 at 5:25













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%2f53993021%2fhow-do-i-get-a-value-for-a-non-pk-field-in-a-fk%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














Use values() method



Target.objects.filter(status__status_open=1).values('last_name', 'first_name', 'status__status_text')





share|improve this answer
























  • Using the django-rest-framework, how do I add that to the serializer?

    – Bring Coffee Bring Beer
    Jan 1 at 5:24











  • pls do add your current configuration of serializer along with current output and required output :)

    – JPG
    Jan 1 at 5:25


















0














Use values() method



Target.objects.filter(status__status_open=1).values('last_name', 'first_name', 'status__status_text')





share|improve this answer
























  • Using the django-rest-framework, how do I add that to the serializer?

    – Bring Coffee Bring Beer
    Jan 1 at 5:24











  • pls do add your current configuration of serializer along with current output and required output :)

    – JPG
    Jan 1 at 5:25
















0












0








0







Use values() method



Target.objects.filter(status__status_open=1).values('last_name', 'first_name', 'status__status_text')





share|improve this answer













Use values() method



Target.objects.filter(status__status_open=1).values('last_name', 'first_name', 'status__status_text')






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 5:10









JPGJPG

14.2k2933




14.2k2933













  • Using the django-rest-framework, how do I add that to the serializer?

    – Bring Coffee Bring Beer
    Jan 1 at 5:24











  • pls do add your current configuration of serializer along with current output and required output :)

    – JPG
    Jan 1 at 5:25





















  • Using the django-rest-framework, how do I add that to the serializer?

    – Bring Coffee Bring Beer
    Jan 1 at 5:24











  • pls do add your current configuration of serializer along with current output and required output :)

    – JPG
    Jan 1 at 5:25



















Using the django-rest-framework, how do I add that to the serializer?

– Bring Coffee Bring Beer
Jan 1 at 5:24





Using the django-rest-framework, how do I add that to the serializer?

– Bring Coffee Bring Beer
Jan 1 at 5:24













pls do add your current configuration of serializer along with current output and required output :)

– JPG
Jan 1 at 5:25







pls do add your current configuration of serializer along with current output and required output :)

– JPG
Jan 1 at 5:25






















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%2f53993021%2fhow-do-i-get-a-value-for-a-non-pk-field-in-a-fk%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

The term 'EXEC' is not recognized as the name of a cmdlet Powershell

NPM command prompt closes immediately [closed]

Error binding properties and functions in emscripten