1024programmer Java How to create blank model object in django? -HowdoIcreateablankmodelobjectindjango?

How to create blank model object in django? -HowdoIcreateablankmodelobjectindjango?

I’m trying to build a table in template that shows the latest entry to a particular table that meets certain criteria, specifically that qor_json is not null or “”. In some cases, for a given tool and context, there’s no record that matches the criteria. Still, since I want a table, I need something to go there so I’m trying to pass in a blank object somehow. The part of the code below (which I’m providing all of in order to give context), is:

I’m trying to build a table in a template that displays the latest entries from a specific table that meet certain criteria, specifically that qor_json is not null or “”. In some cases, there are no matching records for a given tool and context. However, since I want a table, I need something to go there, so I’m trying to pass a blank object somehow. The section of code below (I’m providing everything to provide context) is:

if action:
     print c, t+":", action[0].qor_json, action[0].time
     acts.append(action[0])
 else:
     acts.append(None)
 

Below is the full view. What I’m trying to do is be able to display a blank table cell in cases where there’s no matching object, but display an arbitrary attribute of the object if it does.

Here is the full view. What I want to do is be able to display a blank table cell if there is no matching object, but if there is, display arbitrary properties of the object.

def index(request):
     t = loader.get_template('index.html')
     all_cOntexts= md.Contexts.objects.all().order_by('name')
     all_tools = md.Tools.objects.all().order_by('name')
     latest_actiOns= (md.Actions.objects
                       .values('tool', 'context')
                       .annotate(max_id=Max('id'))
                      )

     actiOns= md.Actions.objects.exclude(qor_json__isnull=True).exclude(
         qor_json__in=[""]).filter(id__in=[
         a['max_id'] for a in latest_actions
     ])

     set_of_tools = sorted(set(a.tool.name for a in actions))
     set_of_cOntexts= sorted(set(a.context.name for a in actions))

     names = []
     act_rows = []

     for c in set_of_contexts:
         acts = []
         for t in set_of_tools:
             action = filter(
                 lambda a: a.tool.name==t and a.context.name==c, actions)
             if action:
                 names.append(action[0].time)
                 acts.append(action[0])
             else:
                 names.append(None)
                 acts.append(None)

         act_rows.append(acts)

     cOntext= RequestContext(request, {
         'contexts' : all_contexts,
         'tools' : all_tools,
         'bundles' : zip(names, act_rows),
         'ct' : 25,
     })
 

This is the index.html template.

This is the index.html template.

 {# Contexts on y-axis, Tools on x-axis #}
   {% for tl in tools %}
   
   {% endfor %}
 
 {% for bundle in bundles %}
   
     {% for action in bundle.1 %}
        
     {% endfor %}
   
 {% endfor %}
 
{{ tl.name }}
{{ bundle.0.qor_json }}{{ action.qor_json }}

My thought was that if I could pass a blank Action object in, then action.qor_json would just print nothing out for the ones where there was no data, but I can’t figure out how to create a blank object, because it’s from a model which only pulls things from the database. Is there a better way to achieve what I’m trying to achieve?

The idea is that if I could pass in a blank Action object, then action.qor_json would just print out the ones with no data, but I can’t get it to work It’s clear how to create a blank object since it comes from a model that just pulls stuff from the database. Is there a better way to achieve what I’m trying to achieve?

Thanks!

1 solution

#1


if action:
     print c, t+":", action[0].qor_json, action[0].time
     acts.append(action[0])
 else:
     acts.append(md.Actions())
 

Django convention would be to call your model Action, not Actions, by the way.

Django convention is to call your model Actions by the way, rather than Actions.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/756373

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索