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.










share|improve this question




















  • 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















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.










share|improve this question




















  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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. 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














  • 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








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

















active

oldest

votes











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%2f53248663%2fproblems-with-related-name-with-django%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





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.




draft saved


draft discarded














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





















































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

Coverage of Google Street View

Full-time equivalent

Surfing