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.
django django-models
add a comment |
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.
django django-models
What's wrong withself.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 itc
, you can callprint(c.question)
to see what happens. You can also tryprint(c.question_id_id)
, which should result in an error, andprint(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
add a comment |
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.
django django-models
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
django django-models
edited Nov 7 at 19:13
asked Nov 7 at 19:07
Ammar Al-Taweel
33
33
What's wrong withself.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 itc
, you can callprint(c.question)
to see what happens. You can also tryprint(c.question_id_id)
, which should result in an error, andprint(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
add a comment |
What's wrong withself.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 itc
, you can callprint(c.question)
to see what happens. You can also tryprint(c.question_id_id)
, which should result in an error, andprint(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
add a comment |
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))
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
add a comment |
up vote
-1
down vote
Try this
def __str__(self):
return 'Choice for Q: {}'.format(self.question.__str__())
1
You don't need the last.__str__()
– thebjorn
Nov 7 at 19:40
add a comment |
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))
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
add a comment |
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))
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
add a comment |
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))
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))
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
add a comment |
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
add a comment |
up vote
-1
down vote
Try this
def __str__(self):
return 'Choice for Q: {}'.format(self.question.__str__())
1
You don't need the last.__str__()
– thebjorn
Nov 7 at 19:40
add a comment |
up vote
-1
down vote
Try this
def __str__(self):
return 'Choice for Q: {}'.format(self.question.__str__())
1
You don't need the last.__str__()
– thebjorn
Nov 7 at 19:40
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Try this
def __str__(self):
return 'Choice for Q: {}'.format(self.question.__str__())
Try this
def __str__(self):
return 'Choice for Q: {}'.format(self.question.__str__())
answered Nov 7 at 19:29
some_code
1144
1144
1
You don't need the last.__str__()
– thebjorn
Nov 7 at 19:40
add a comment |
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
add a comment |
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%2f53196164%2freferring-from-the-many-field-to-the-one-in-django%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
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 callprint(c.question)
to see what happens. You can also tryprint(c.question_id_id)
, which should result in an error, andprint(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