0

I need to do a lot of popup select windows in Djago application, basically for each model and show only some selected fields (each time different). I would like make universal view, which would get model name, list of field names and make such popup-select.

Something like that (simplyfied):

def MyView(request,model_name,field_names):
  a_model=get_model(model_name)
  paginator = Paginator(a_model.objects.order_by('id'),30)
  values = paginator.page(1)
  contex={'values':values, 'field_names':field_names}
  return render(request, 'popup.html',context)

popup.html:

<tr>
{% for fn in field_names %}<th>{{fn}}</th>{% endfor %}
</tr>
{% for val in values %}<tr>
{% for fn in field_names %}<td>{{val.'fn'}}</td>{% endfor %} // val.'fn' does not work
</tr>{% endfor %}

val.'fn' does not work, but I would like something to this effect, to get result of

MyView(request,'Friends',('name','nick','detail123'))

name | nick | detail123 
-----+------+----------
James| Joe  | horses
Peter| Pet  | cars
........

Thanks for help

gilhad
  • 609
  • 1
  • 5
  • 22
  • 1
    You need to do this processing in the *view*, not in the *template*. So construct a list of lists that contain the data. – Willem Van Onsem Oct 23 '18 at 14:21
  • What about displaying your info as {{ form.as_table }}? – Carl Brubaker Oct 23 '18 at 14:35
  • @CarlBrubaker the problem is, that only few fields should be used (there are like detail001 .. detail200, but I need just detail123 here and detail052 on another call) – gilhad Oct 23 '18 at 14:48
  • @WillemVanOnsem that was my first impulse, if nothing better would be available, but I am not sure, if something like that does NOT already exists (as well as I could write my own Paginator, but there is one in django.core.paginator so it is better to use something developed and tested already) – gilhad Oct 23 '18 at 14:51
  • I think @Anentropic is right. You have to incorporate `getattr()` into your view and collect your specific field values. – Carl Brubaker Oct 23 '18 at 16:28
  • I think so :) If @Anentropic made it to an aswer, I would accept it as solution :) Better, than prepare values in view for cases, when template also vants to do something with original objects :) – gilhad Oct 24 '18 at 08:50
  • Sorry, missed, that is already marked as duplikate. It really is, just the name a motivation differs. Accepting duplicate. – gilhad Oct 24 '18 at 10:32

0 Answers0