django how to following relationships backwards?

2024/11/17 0:16:42

I am having some issue with following relationships backwards. From the parent page i want to be able to see what children belong to that parent. Heres what i got so far

model.py

class Parents(models.Model):name = models.CharField()class Child(models.Model):parent = models.ForeignKey(Parent, related_name='children')child_name = models.CharField()

views.py

def parents(request):return render_to_response('dashboard/parent.html', {'parents': Parents.objects.all() })

parent.html

{% for parent in parents %}<tr><td><a href="/parent/get/{{ parent.id }}/">{{ parent.id }}</a></td><td><a href="/parent/get/{{ parent.id }}/">{{ parant.name }}</a></td><td><a href="/parent/get/{{ parent.id }}/">{{ parent.children.child_name }}</a></td></tr>{% endfor %}</tbody></table></div>
{% endblock %}
Answer

As Daniel Roseman mentioned you possibily have more than one child per parent, thus you have to get the whole set of children and iterate over it.

You can get this set with: parent.children_set.all()

https://en.xdnf.cn/q/119128.html

Related Q&A

Python File handling: Seaching for specific numbers

Im creating a document in which I need to record license plates of vehicles (its a practice exercise, nothing illegal) and calculate the speed they travel at and display all the vehicles that are trave…

How to convert token list into wordnet lemma list using nltk?

I have a list of tokens extracted out of a pdf source. I am able to pre process the text and tokenize it but I want to loop through the tokens and convert each token in the list to its lemma in the wor…

Script throws an error when it is made to run using multiprocessing

Ive written a script in python in combination with BeautifulSoup to extract the title of books which get populated upon providing some ISBN numbers in amazon search box. Im providing those ISBN numbers…

Efficiently pair random elements of list

I have a list of n elements say: foo = [a, b, c, d, e] I would like to randomly pair elements of this list to receive for example: bar = [[a, c], [b, e]] where the last element will be discarded if the…

ALL permutations of a list with repetition but not doubles

I have seen similar but not the same: here. I definitely want the permutations, not combinations, of all list elements. Mine is different because itertools permutation of a,b,c returns abc but not aba …

NameError: name current_portfolio is not defined

I am getting NameError: name current_portfolio is not defineddef initialize(context): context.sym = symbol(xxx) context.i = 0def handle_data(context, data):context.i += 1 if context.i < 60:returnsma…

Scrape an Ajax form with .submit() with Python and Selenium

I am trying to get the link from a web page. The web page sends the request using javascript, then the server sends a response which goes directly to download a PDF. This new PDF is automatically downl…

How to process break an array in Python?

I would like to use a double array. But I still fail to do it. This what I did. Folder = "D:\folder" Name = [gadfg5, 546sfdgh] Ver = [None, hhdt5463]for dn in Name :for dr in Ver :if dr is No…

Why am I getting replacement index 1 out of range for positional args tuple error

I keep getting this error: Replacement index 1 out of range for positional args tuple on this line of code: print("{1}, {2}, {3}, {4}".format(question[3]), question[4], question[5], question[…

Python: Find keywords in a text file from another text file

Take this invoice.txt for exampleInvoice NumberINV-3337Order Number12345Invoice DateJanuary 25, 2016Due DateJanuary 31, 2016And this is what dict.txt looks like:Invoice DateInvoice NumberDue DateOrder …