Problems with related_name with Django
up vote
0
down vote
favorite
I have problems with relate_name on my model. On the class 'Pildora' there is a ForeignKey on the field: bloque. I have indicated the relate_name, but it doesn't work.
I have these models:
class Bloque(models.Model):
titulo = models.CharField(max_length=200)
letra = models.CharField(max_length=1)
descripcion = models.CharField(max_length=200)
tema = models.ForeignKey(Tema, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(max_length=1)
updated_at = models.DateTimeField(auto_now=True)
class Pildora(models.Model):
titulo = models.CharField(max_length=200)
descripcion = RichTextField(max_length=2000, config_name='default')
slug = models.SlugField(max_length=20)
url = models.URLField(max_length=200)
tipo = models.ForeignKey(TipoPildora, on_delete=models.CASCADE)
identificador = models.IntegerField()
pildora_anterior = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True)
bloque = models.ForeignKey(Bloque, on_delete=models.CASCADE, related_name='%(app_label)s_%(class)s_related')
activo = models.BooleanField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
But when i use:
bloque = Bloque.objects.filter(tema__asignatura__slug=kwargs["asignatura"], tema__slug=kwargs["tema"], slug=kwargs["bloque"])
print(bloque.formacion_pildora_related)
Django says:
AttributeError: 'QuerySet' object has no attribute 'formacion_pildora_related'
I read the django doc, but I can't find why is this happening :S
What could i do?
PS: In the case in question %(app_label)s_%(class)s_related refers to 'formacion_pildora_bloque'. I have put fixed names like 'pildoras' or even left it unreported, so that by default it is used as 'set_pillora', but none of this works.
Thank you so much all, really. I'm stuck and a little frustrated.
python django django-models
add a comment |
up vote
0
down vote
favorite
I have problems with relate_name on my model. On the class 'Pildora' there is a ForeignKey on the field: bloque. I have indicated the relate_name, but it doesn't work.
I have these models:
class Bloque(models.Model):
titulo = models.CharField(max_length=200)
letra = models.CharField(max_length=1)
descripcion = models.CharField(max_length=200)
tema = models.ForeignKey(Tema, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(max_length=1)
updated_at = models.DateTimeField(auto_now=True)
class Pildora(models.Model):
titulo = models.CharField(max_length=200)
descripcion = RichTextField(max_length=2000, config_name='default')
slug = models.SlugField(max_length=20)
url = models.URLField(max_length=200)
tipo = models.ForeignKey(TipoPildora, on_delete=models.CASCADE)
identificador = models.IntegerField()
pildora_anterior = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True)
bloque = models.ForeignKey(Bloque, on_delete=models.CASCADE, related_name='%(app_label)s_%(class)s_related')
activo = models.BooleanField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
But when i use:
bloque = Bloque.objects.filter(tema__asignatura__slug=kwargs["asignatura"], tema__slug=kwargs["tema"], slug=kwargs["bloque"])
print(bloque.formacion_pildora_related)
Django says:
AttributeError: 'QuerySet' object has no attribute 'formacion_pildora_related'
I read the django doc, but I can't find why is this happening :S
What could i do?
PS: In the case in question %(app_label)s_%(class)s_related refers to 'formacion_pildora_bloque'. I have put fixed names like 'pildoras' or even left it unreported, so that by default it is used as 'set_pillora', but none of this works.
Thank you so much all, really. I'm stuck and a little frustrated.
python django django-models
2
What output do you expect here? The related attribute is on individual model instances, not on the queryset. Tryprint(bloque[0].formacion_pildora_related)
– Håken Lid
Nov 11 at 12:19
As I read in the documentation, by using relate_name you can bring back information from the foreignkey related model. Isn't that right?
– jocafernanro
Nov 11 at 12:21
Oh, it works!!! I didn't expect that. Thank you so much, Haken Lid. You helped me a lot!!
– jocafernanro
Nov 11 at 12:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have problems with relate_name on my model. On the class 'Pildora' there is a ForeignKey on the field: bloque. I have indicated the relate_name, but it doesn't work.
I have these models:
class Bloque(models.Model):
titulo = models.CharField(max_length=200)
letra = models.CharField(max_length=1)
descripcion = models.CharField(max_length=200)
tema = models.ForeignKey(Tema, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(max_length=1)
updated_at = models.DateTimeField(auto_now=True)
class Pildora(models.Model):
titulo = models.CharField(max_length=200)
descripcion = RichTextField(max_length=2000, config_name='default')
slug = models.SlugField(max_length=20)
url = models.URLField(max_length=200)
tipo = models.ForeignKey(TipoPildora, on_delete=models.CASCADE)
identificador = models.IntegerField()
pildora_anterior = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True)
bloque = models.ForeignKey(Bloque, on_delete=models.CASCADE, related_name='%(app_label)s_%(class)s_related')
activo = models.BooleanField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
But when i use:
bloque = Bloque.objects.filter(tema__asignatura__slug=kwargs["asignatura"], tema__slug=kwargs["tema"], slug=kwargs["bloque"])
print(bloque.formacion_pildora_related)
Django says:
AttributeError: 'QuerySet' object has no attribute 'formacion_pildora_related'
I read the django doc, but I can't find why is this happening :S
What could i do?
PS: In the case in question %(app_label)s_%(class)s_related refers to 'formacion_pildora_bloque'. I have put fixed names like 'pildoras' or even left it unreported, so that by default it is used as 'set_pillora', but none of this works.
Thank you so much all, really. I'm stuck and a little frustrated.
python django django-models
I have problems with relate_name on my model. On the class 'Pildora' there is a ForeignKey on the field: bloque. I have indicated the relate_name, but it doesn't work.
I have these models:
class Bloque(models.Model):
titulo = models.CharField(max_length=200)
letra = models.CharField(max_length=1)
descripcion = models.CharField(max_length=200)
tema = models.ForeignKey(Tema, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(max_length=1)
updated_at = models.DateTimeField(auto_now=True)
class Pildora(models.Model):
titulo = models.CharField(max_length=200)
descripcion = RichTextField(max_length=2000, config_name='default')
slug = models.SlugField(max_length=20)
url = models.URLField(max_length=200)
tipo = models.ForeignKey(TipoPildora, on_delete=models.CASCADE)
identificador = models.IntegerField()
pildora_anterior = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True)
bloque = models.ForeignKey(Bloque, on_delete=models.CASCADE, related_name='%(app_label)s_%(class)s_related')
activo = models.BooleanField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
But when i use:
bloque = Bloque.objects.filter(tema__asignatura__slug=kwargs["asignatura"], tema__slug=kwargs["tema"], slug=kwargs["bloque"])
print(bloque.formacion_pildora_related)
Django says:
AttributeError: 'QuerySet' object has no attribute 'formacion_pildora_related'
I read the django doc, but I can't find why is this happening :S
What could i do?
PS: In the case in question %(app_label)s_%(class)s_related refers to 'formacion_pildora_bloque'. I have put fixed names like 'pildoras' or even left it unreported, so that by default it is used as 'set_pillora', but none of this works.
Thank you so much all, really. I'm stuck and a little frustrated.
python django django-models
python django django-models
edited Nov 11 at 12:21
Håken Lid
10.4k62441
10.4k62441
asked Nov 11 at 12:16
jocafernanro
11
11
2
What output do you expect here? The related attribute is on individual model instances, not on the queryset. Tryprint(bloque[0].formacion_pildora_related)
– Håken Lid
Nov 11 at 12:19
As I read in the documentation, by using relate_name you can bring back information from the foreignkey related model. Isn't that right?
– jocafernanro
Nov 11 at 12:21
Oh, it works!!! I didn't expect that. Thank you so much, Haken Lid. You helped me a lot!!
– jocafernanro
Nov 11 at 12:24
add a comment |
2
What output do you expect here? The related attribute is on individual model instances, not on the queryset. Tryprint(bloque[0].formacion_pildora_related)
– Håken Lid
Nov 11 at 12:19
As I read in the documentation, by using relate_name you can bring back information from the foreignkey related model. Isn't that right?
– jocafernanro
Nov 11 at 12:21
Oh, it works!!! I didn't expect that. Thank you so much, Haken Lid. You helped me a lot!!
– jocafernanro
Nov 11 at 12:24
2
2
What output do you expect here? The related attribute is on individual model instances, not on the queryset. Try
print(bloque[0].formacion_pildora_related)– Håken Lid
Nov 11 at 12:19
What output do you expect here? The related attribute is on individual model instances, not on the queryset. Try
print(bloque[0].formacion_pildora_related)– Håken Lid
Nov 11 at 12:19
As I read in the documentation, by using relate_name you can bring back information from the foreignkey related model. Isn't that right?
– jocafernanro
Nov 11 at 12:21
As I read in the documentation, by using relate_name you can bring back information from the foreignkey related model. Isn't that right?
– jocafernanro
Nov 11 at 12:21
Oh, it works!!! I didn't expect that. Thank you so much, Haken Lid. You helped me a lot!!
– jocafernanro
Nov 11 at 12:24
Oh, it works!!! I didn't expect that. Thank you so much, Haken Lid. You helped me a lot!!
– jocafernanro
Nov 11 at 12:24
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53248663%2fproblems-with-related-name-with-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
2
What output do you expect here? The related attribute is on individual model instances, not on the queryset. Try
print(bloque[0].formacion_pildora_related)– Håken Lid
Nov 11 at 12:19
As I read in the documentation, by using relate_name you can bring back information from the foreignkey related model. Isn't that right?
– jocafernanro
Nov 11 at 12:21
Oh, it works!!! I didn't expect that. Thank you so much, Haken Lid. You helped me a lot!!
– jocafernanro
Nov 11 at 12:24