Python inheritance question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python inheritance question

Hello, pythoneers! ^^ A question about Python/Django: I have a parent class Page which has fields like: Title, etc. and have another child class Services(Page) . My view is: def serviceDetails(request, Slug): obj=Services.objects.filter(Slug=Slug) context = { 'obj' : obj, } return render(request, 'home/our-service-details.html',context) How can I access the fields of the parent class Page in my the template? {{obj.Title}} does not work

26th Mar 2022, 5:07 PM
Saidmamad Gulomshoev
Saidmamad Gulomshoev - avatar
3 Answers
+ 1
Yes, Akshay, I solved the problem, and you're right! The mistake was to use filter instead of get(). Thank you!
23rd Apr 2022, 5:09 AM
Saidmamad Gulomshoev
Saidmamad Gulomshoev - avatar
0
Or should I add Foreign Key Field ?
26th Mar 2022, 5:10 PM
Saidmamad Gulomshoev
Saidmamad Gulomshoev - avatar
0
Sorry for late reply. I don't know whether your problem is solved or not but I can see you are using `filter` method and this returns a queryset. So {{obj.Title}} will not work directly, firstly you have to access the inner elements of queryset and then use template tag. {% for o in obj %} {{o.Title}} {% endfor %}
23rd Apr 2022, 5:06 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar