Referring from the “many” field to the “one” in Django











up vote
0
down vote

favorite












A noob question.
I have followed the official Django tutorial and I have the following:



class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def __str__(self):
return self.question_text

class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

def __str__(self):
return self.choice_text


I have tried replacing the Choice.str() code as follows:



def __str__(self):
return 'Choice for Q: {}'.format(Question.objects.get(pk=self.question_id_id).__str__())


I am trying to view the question related to this choise (when viewed in the admin).
The code doesn't work. What is the correct way of doing this?



Many thanks.










share|improve this question
























  • What's wrong with self.question?
    – thebjorn
    Nov 7 at 19:19










  • This could be clarified if you try some of these things in the Django shell. If you load up a Choice object, call it c, you can call print(c.question) to see what happens. You can also try print(c.question_id_id), which should result in an error, and print(c.question_id), which should result in the ID of the related question, a.k.a. c.question.id.
    – kungphu
    Nov 8 at 5:34















up vote
0
down vote

favorite












A noob question.
I have followed the official Django tutorial and I have the following:



class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def __str__(self):
return self.question_text

class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

def __str__(self):
return self.choice_text


I have tried replacing the Choice.str() code as follows:



def __str__(self):
return 'Choice for Q: {}'.format(Question.objects.get(pk=self.question_id_id).__str__())


I am trying to view the question related to this choise (when viewed in the admin).
The code doesn't work. What is the correct way of doing this?



Many thanks.










share|improve this question
























  • What's wrong with self.question?
    – thebjorn
    Nov 7 at 19:19










  • This could be clarified if you try some of these things in the Django shell. If you load up a Choice object, call it c, you can call print(c.question) to see what happens. You can also try print(c.question_id_id), which should result in an error, and print(c.question_id), which should result in the ID of the related question, a.k.a. c.question.id.
    – kungphu
    Nov 8 at 5:34













up vote
0
down vote

favorite









up vote
0
down vote

favorite











A noob question.
I have followed the official Django tutorial and I have the following:



class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def __str__(self):
return self.question_text

class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

def __str__(self):
return self.choice_text


I have tried replacing the Choice.str() code as follows:



def __str__(self):
return 'Choice for Q: {}'.format(Question.objects.get(pk=self.question_id_id).__str__())


I am trying to view the question related to this choise (when viewed in the admin).
The code doesn't work. What is the correct way of doing this?



Many thanks.










share|improve this question















A noob question.
I have followed the official Django tutorial and I have the following:



class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def __str__(self):
return self.question_text

class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

def __str__(self):
return self.choice_text


I have tried replacing the Choice.str() code as follows:



def __str__(self):
return 'Choice for Q: {}'.format(Question.objects.get(pk=self.question_id_id).__str__())


I am trying to view the question related to this choise (when viewed in the admin).
The code doesn't work. What is the correct way of doing this?



Many thanks.







django django-models






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 19:13

























asked Nov 7 at 19:07









Ammar Al-Taweel

33




33












  • What's wrong with self.question?
    – thebjorn
    Nov 7 at 19:19










  • This could be clarified if you try some of these things in the Django shell. If you load up a Choice object, call it c, you can call print(c.question) to see what happens. You can also try print(c.question_id_id), which should result in an error, and print(c.question_id), which should result in the ID of the related question, a.k.a. c.question.id.
    – kungphu
    Nov 8 at 5:34


















  • What's wrong with self.question?
    – thebjorn
    Nov 7 at 19:19










  • This could be clarified if you try some of these things in the Django shell. If you load up a Choice object, call it c, you can call print(c.question) to see what happens. You can also try print(c.question_id_id), which should result in an error, and print(c.question_id), which should result in the ID of the related question, a.k.a. c.question.id.
    – kungphu
    Nov 8 at 5:34
















What's wrong with self.question?
– thebjorn
Nov 7 at 19:19




What's wrong with self.question?
– thebjorn
Nov 7 at 19:19












This could be clarified if you try some of these things in the Django shell. If you load up a Choice object, call it c, you can call print(c.question) to see what happens. You can also try print(c.question_id_id), which should result in an error, and print(c.question_id), which should result in the ID of the related question, a.k.a. c.question.id.
– kungphu
Nov 8 at 5:34




This could be clarified if you try some of these things in the Django shell. If you load up a Choice object, call it c, you can call print(c.question) to see what happens. You can also try print(c.question_id_id), which should result in an error, and print(c.question_id), which should result in the ID of the related question, a.k.a. c.question.id.
– kungphu
Nov 8 at 5:34












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










Firstly, you don't need to do an extra query inside the str method.
Secondly, the string formatting needs a string. You are passing a Question object's str method, that is you're basically passing a reference for this function object (Yes, everything is an object in python)



This snippet should solve your issue.



def __str__(self):
return 'Choice for Q: {}'.format(str(self.question.id))





share|improve this answer























  • While this might answer the authors' question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
    – Roy Scheffers
    Nov 8 at 0:43










  • Thanks for the simple answer. It did need an extra ')' at the end. The reason I couldn't see this simple answer before is that I added the following line in the str function: import ipdb;ipdb.set_trace() Then I tried to print the available fields in 'self' by typing: pp self.__dict__ that gave me: {'_state': <django.db.models.base.ModelState object at 0x000001E565FCBFD0>, 'choice_text': 'Everything', 'id': 2, 'question_id': 1, 'votes': 0} So I couldn't see how the Choice will refer back to the question. It seems I was over complicating a simple issue.
    – Ammar Al-Taweel
    Nov 8 at 12:29




















up vote
-1
down vote













Try this



def __str__(self):
return 'Choice for Q: {}'.format(self.question.__str__())





share|improve this answer

















  • 1




    You don't need the last .__str__()
    – thebjorn
    Nov 7 at 19:40











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',
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%2f53196164%2freferring-from-the-many-field-to-the-one-in-django%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Firstly, you don't need to do an extra query inside the str method.
Secondly, the string formatting needs a string. You are passing a Question object's str method, that is you're basically passing a reference for this function object (Yes, everything is an object in python)



This snippet should solve your issue.



def __str__(self):
return 'Choice for Q: {}'.format(str(self.question.id))





share|improve this answer























  • While this might answer the authors' question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
    – Roy Scheffers
    Nov 8 at 0:43










  • Thanks for the simple answer. It did need an extra ')' at the end. The reason I couldn't see this simple answer before is that I added the following line in the str function: import ipdb;ipdb.set_trace() Then I tried to print the available fields in 'self' by typing: pp self.__dict__ that gave me: {'_state': <django.db.models.base.ModelState object at 0x000001E565FCBFD0>, 'choice_text': 'Everything', 'id': 2, 'question_id': 1, 'votes': 0} So I couldn't see how the Choice will refer back to the question. It seems I was over complicating a simple issue.
    – Ammar Al-Taweel
    Nov 8 at 12:29

















up vote
0
down vote



accepted










Firstly, you don't need to do an extra query inside the str method.
Secondly, the string formatting needs a string. You are passing a Question object's str method, that is you're basically passing a reference for this function object (Yes, everything is an object in python)



This snippet should solve your issue.



def __str__(self):
return 'Choice for Q: {}'.format(str(self.question.id))





share|improve this answer























  • While this might answer the authors' question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
    – Roy Scheffers
    Nov 8 at 0:43










  • Thanks for the simple answer. It did need an extra ')' at the end. The reason I couldn't see this simple answer before is that I added the following line in the str function: import ipdb;ipdb.set_trace() Then I tried to print the available fields in 'self' by typing: pp self.__dict__ that gave me: {'_state': <django.db.models.base.ModelState object at 0x000001E565FCBFD0>, 'choice_text': 'Everything', 'id': 2, 'question_id': 1, 'votes': 0} So I couldn't see how the Choice will refer back to the question. It seems I was over complicating a simple issue.
    – Ammar Al-Taweel
    Nov 8 at 12:29















up vote
0
down vote



accepted







up vote
0
down vote



accepted






Firstly, you don't need to do an extra query inside the str method.
Secondly, the string formatting needs a string. You are passing a Question object's str method, that is you're basically passing a reference for this function object (Yes, everything is an object in python)



This snippet should solve your issue.



def __str__(self):
return 'Choice for Q: {}'.format(str(self.question.id))





share|improve this answer














Firstly, you don't need to do an extra query inside the str method.
Secondly, the string formatting needs a string. You are passing a Question object's str method, that is you're basically passing a reference for this function object (Yes, everything is an object in python)



This snippet should solve your issue.



def __str__(self):
return 'Choice for Q: {}'.format(str(self.question.id))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 18:45

























answered Nov 7 at 21:52









Mehran

218110




218110












  • While this might answer the authors' question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
    – Roy Scheffers
    Nov 8 at 0:43










  • Thanks for the simple answer. It did need an extra ')' at the end. The reason I couldn't see this simple answer before is that I added the following line in the str function: import ipdb;ipdb.set_trace() Then I tried to print the available fields in 'self' by typing: pp self.__dict__ that gave me: {'_state': <django.db.models.base.ModelState object at 0x000001E565FCBFD0>, 'choice_text': 'Everything', 'id': 2, 'question_id': 1, 'votes': 0} So I couldn't see how the Choice will refer back to the question. It seems I was over complicating a simple issue.
    – Ammar Al-Taweel
    Nov 8 at 12:29




















  • While this might answer the authors' question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
    – Roy Scheffers
    Nov 8 at 0:43










  • Thanks for the simple answer. It did need an extra ')' at the end. The reason I couldn't see this simple answer before is that I added the following line in the str function: import ipdb;ipdb.set_trace() Then I tried to print the available fields in 'self' by typing: pp self.__dict__ that gave me: {'_state': <django.db.models.base.ModelState object at 0x000001E565FCBFD0>, 'choice_text': 'Everything', 'id': 2, 'question_id': 1, 'votes': 0} So I couldn't see how the Choice will refer back to the question. It seems I was over complicating a simple issue.
    – Ammar Al-Taweel
    Nov 8 at 12:29


















While this might answer the authors' question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– Roy Scheffers
Nov 8 at 0:43




While this might answer the authors' question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– Roy Scheffers
Nov 8 at 0:43












Thanks for the simple answer. It did need an extra ')' at the end. The reason I couldn't see this simple answer before is that I added the following line in the str function: import ipdb;ipdb.set_trace() Then I tried to print the available fields in 'self' by typing: pp self.__dict__ that gave me: {'_state': <django.db.models.base.ModelState object at 0x000001E565FCBFD0>, 'choice_text': 'Everything', 'id': 2, 'question_id': 1, 'votes': 0} So I couldn't see how the Choice will refer back to the question. It seems I was over complicating a simple issue.
– Ammar Al-Taweel
Nov 8 at 12:29






Thanks for the simple answer. It did need an extra ')' at the end. The reason I couldn't see this simple answer before is that I added the following line in the str function: import ipdb;ipdb.set_trace() Then I tried to print the available fields in 'self' by typing: pp self.__dict__ that gave me: {'_state': <django.db.models.base.ModelState object at 0x000001E565FCBFD0>, 'choice_text': 'Everything', 'id': 2, 'question_id': 1, 'votes': 0} So I couldn't see how the Choice will refer back to the question. It seems I was over complicating a simple issue.
– Ammar Al-Taweel
Nov 8 at 12:29














up vote
-1
down vote













Try this



def __str__(self):
return 'Choice for Q: {}'.format(self.question.__str__())





share|improve this answer

















  • 1




    You don't need the last .__str__()
    – thebjorn
    Nov 7 at 19:40















up vote
-1
down vote













Try this



def __str__(self):
return 'Choice for Q: {}'.format(self.question.__str__())





share|improve this answer

















  • 1




    You don't need the last .__str__()
    – thebjorn
    Nov 7 at 19:40













up vote
-1
down vote










up vote
-1
down vote









Try this



def __str__(self):
return 'Choice for Q: {}'.format(self.question.__str__())





share|improve this answer












Try this



def __str__(self):
return 'Choice for Q: {}'.format(self.question.__str__())






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 19:29









some_code

1144




1144








  • 1




    You don't need the last .__str__()
    – thebjorn
    Nov 7 at 19:40














  • 1




    You don't need the last .__str__()
    – thebjorn
    Nov 7 at 19:40








1




1




You don't need the last .__str__()
– thebjorn
Nov 7 at 19:40




You don't need the last .__str__()
– thebjorn
Nov 7 at 19:40


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196164%2freferring-from-the-many-field-to-the-one-in-django%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()