[Answered ]-Django Foreign Key many to one not working

1👍

You can try Lookups that span relationships method in this document. Simple add the name of table for more explicit. (or google that term for more detail information)

With this sample model:

class Entry(models.Model):
    blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
    headline = models.CharField(max_length=255)

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

Blog.objects.filter(entry__headline__contains='Lennon')

If you would stick with your way, I suggest you print out the raw query and see what wrong and trace the solution after that.

q = Query.objects.values('val1','val2','val_etc')
print(q.query)

Try the query first if It is correct in all ways, before developing further. Or else you gonna lose tracks and do not know which step is incorrect.

Leave a comment