Django customize model fields to show on template
<p>I'm in a weird situation. I have following model. On a template i render only name and author. The thing is there is <strong>edit display</strong> button, when click on edit display. It should show all the fields name to choose. When i choose fields and save table's element should be updated as chosen fields.</p>

<p>models.py</p>

<pre><code>class Finding(models.Model):
    name = models.CharField()
    isbn = models.CharField()
    author = models.ForeignKey()
    created = models.DateTimeField()
    shop = models.ManyToManyField()
</code></pre>

<p>template(findings.html)</p>

<pre><code>&lt;table&gt;
    &lt;tr&gt;
        &lt;td&gt;{{name}}&lt;/td&gt;
        &lt;td&gt;{{author}}&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
</code></pre>

<p>I was thinking about using a session. But this should be dynamic and permanent. Every user may change showing fields according to their preference. How can i achieve this.</p>

<p>You can create a checkedbox form and handle the values in the view to return appropriate html content, for example (form):</p>

<pre><code>class CheckBox(forms.Form):
    name = forms.BooleanField(required=False)
</code></pre>

<p>View:</p>

<pre><code>def your_view(request):
    form = MyForm(request.POST or None)
    if request.method == "POST":
        if form.is_valid():

            if request.POST["name"]:
                name = 1

            if request.POST["author"]:
                author = 1

return render(request, 'app/findings.html', {'name':name, 'author':author})
</code></pre>

<p>Then in your template, you can use django template if statement to check which has been selected, for example:</p>

<pre><code>{% if name == 1 %}
 &lt;td&gt;{{name}}&lt;/td&gt;
{% endif %}
{% if author == 1 %}
 &lt;td&gt;{{author}}&lt;/td&gt;
{% endif %}
</code></pre>

<p>I hope this gives you some direction.</p>

python django