Sunday, December 21, 2014

Re: Accessing Models After get_model()

Have you enabled admindocs?

Might be the solution you are looking for.

Connected by Motorola


llanitedave <llanitedave@birdandflower.com> wrote:

I'm trying to display a list of the models in my app on a web page, similar to the way the Django Admin does it.  I'm not sure where the admin section is coded, which might be part of my problem.

I'm using Django 1.7, and have built a very simple view:

from django.db.models import get_app, get_models 

def index(request):
app = get_app('sampledb')
modellist = get_models(app)
context = {'my_models': modellist}
return render(request, 'sampledb/index.html', context)

in my index.html page I created a table:

{% if my_models %}
    <p>{{ my_models }}</p>
    <table>
        <thead>
    <tr>
        <th scope="col">Name</th>
<th scope="col">Verbose Plural</th>
    </tr>
</thead>
<tbody>
    {% for model in my_models %}
    <tr>
        <td>{{ model.name }}</td>
<td>{{ model.meta.verbose_name_plural }}</td>
    </tr>
    {% endfor %}
</tbody>
    </table>
{% endif %}


When I run this, my webpage first displays the <p>{{ my_models }}</p>, showing a list of model classes, such as <class 'sampledb.models.Sites'>, <class 'sampledb.models.People'>, etc.

I guess that's my first hint that getting attributes from the 'model.name' isn't going to display anything, and it doesn't.  But it doesn't give an error, either.

How do I dig into the class to retrieve the actual model so that I can display its attributes?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b799813c-9a6a-493e-8346-aaf17fca74d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/82kuqyxqb09hy6hsw2asstoa.1419218249175%40email.android.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment