0

so I did some searches but couldn't find the answer to my particular issue... What is happening is that:

I have a model named section which have section_title field. This field can have spaces, for example: "First Section".

When I pass this to Django, I am removing the space in Python with replace(), and created a filter on Django which also removes the space like so:

@register.filter(name='replace_char')
def replace_char(value, arg):
 return value.replace(arg, '')

Then on my template:

{% for subsection in section.section_title|replace_char:" " %}

The issue is that subsection is being shown as each character from section_title, instead of the list that section_title references. Here is the dictionary being passed to the template:

{'sections': [< Section: First Section>, < Section: Second Section>], 'FirstSection': [< Subsection: Subsection 1>, < Subsection: Subsection 2>], 'SecondSection': [< Subsection: Bla1>], 'teste': ['1', '2', '3']}

If I hardcode:

{% for subsection in FirstSection %}

It works...

Any ideas? Thanks!

OBS: I removed the spaces because I thought they were causing the issue, but apparently not. It wasnt working with the spaces as well...

Full template code:

{% for section in sections %}
 <div class="sectionHeader">
 {{ section.section_title }}
 </div>
 <div class="forumSection">
 {% for subsection in section.section_title|replace_char:" " %}
 <div>
 {{ subsection }}
 </div>
 {% endfor %}
 </div>
{% endfor %}

Models:

class Section(models.Model):
 def __str__(self):
 return self.section_title
 section_title = models.CharField(primary_key = True, unique = True, max_length = 50)
class Subsection(models.Model):
 def __str__(self):
 return self.subsection_title
 subsection_title = models.CharField(max_length = 50)
 subsection_section = models.ForeignKey(
 'Section',
 on_delete = models.CASCADE,
 )
asked Dec 23, 2017 at 5:48
7
  • I believe what you are following is the only solution. Commented Dec 23, 2017 at 5:59
  • @AnupYadav, sorry, I didn't understand what you meant. Commented Dec 23, 2017 at 6:01
  • can you show what context variable you are passing from your view? Commented Dec 23, 2017 at 6:09
  • The dictionary being passed to the template doesn't contain "section". Did you mean "sections"? Commented Dec 23, 2017 at 6:10
  • @UmarAsghar, isnt the context variable the one in bold on my first comment? Commented Dec 23, 2017 at 6:13

1 Answer 1

1

You want to set up a related_name on your foreign key so that you can fetch all your subsections corresponding to the section.

class Section(models.Model):
 section_title = models.CharField(primary_key = True, unique = True, max_length = 50)
class Subsection(models.Model):
 subsection_title = models.CharField(max_length = 50)
 subsection_section = models.ForeignKey(
 'Section',
 related_name = 'subsections',
 on_delete = models.CASCADE,
 )

And then you can change your template code to loop over your subsections as follows:

{% for section in sections %}
 <div class="sectionHeader">
 {{ section.section_title }}
 </div>
 <div class="forumSection">
 <div>
 {% for subsection in section.subsections.all %}
 {{ subsection.subsection_title }}
 {% endfor %}
 </div>
 </div>
{% endfor %}

See https://docs.djangoproject.com/en/1.11/topics/db/queries/#related-objects for more information.

answered Dec 23, 2017 at 6:19
Sign up to request clarification or add additional context in comments.

10 Comments

Yes, I want to do the "unless" portion of your comment. However, section is a foreign_key of subsection, meaning that the section doesnt know which subsections it has... Only subsections knows that it is a child of a given Section... Would I necessarily need to change my models in this case?
I will add the models to the question description
Hmm sorry I wasn't quite clear. Can you edit your question with the code for the Section model?
Yeah that would be helpful.
@GuiFGDeo use reverse relation in that case. Update the query to cover prefetch_related case as well for bringing the subsections beside sections.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.