How do I Iterate database object querysets

61 views
Skip to first unread message

G Z

unread,
Jun 24, 2014, 3:04:50 PM6/24/14
to django...@googlegroups.com
I am posting data from a page with a dynamic set of licenses, I'm using the license_name as they key for the post values, then im building a string from the posted values. However
I can't figure out how to iterate through the database object set. There is no other way for what im doing because im using this to insert data into vcenter directly through python code.


ive tried

licenses.Licenses
licenses('Licenses')
licenses['Licenses']
 
current code:

licenses = Licenses.objects.all()
insert_data = request.POST.copy()

for field, lis in licenses:   
      for lisc in insert_data[lis]:
             license_tag += lisc + ','


I've tried

for lis in licenses:   
      for lisc in insert_data[lis]:
             license_tag += lisc + ','

for lis in licenses:   
      for lisc in insert_data[lis['Licenses']]:
             license_tag += lisc + ','

for lis in licenses:   
      for lisc in insert_data[lis('Licenses')]:
             license_tag += lisc + ','

for lis in licenses:   
      for lisc in insert_data[lis.Licenses]:
             license_tag += lisc + ','



licenses looks like, so its an object inside of a list, which means i should be able to access it as lis.objectname or licenses.objectname correct?

licenses
[<Licenses: AGCRM>, <Licenses: BCCPANELVPS>, <Licenses: BCFANTASTICO>, <Licenses: CISCO1000V>, <Licenses: DEAPPASSURECORE>, <Licenses: F5LTM200M>, <Licenses: HYKPS>, <Licenses: HYKPSNODE>, <Licenses: INGVEEAMSER>, <Licenses: INGVEEAMVM>, <Licenses: INGVMWESXI>, <Licenses: INGVMWVC>, <Licenses: LBUTMNS>, <Licenses: LBUTMNWS>, <Licenses: LBUTMWEB>, <Licenses: MSEXCHANGE2013>, <Licenses: MSSERVER2008>, <Licenses: MSSERVER20085CAL>, <Licenses: MSSERVER2012>, <Licenses: MSSQL2008R2>, '...(remaining elements truncated)...']


Message has been deleted

G Z

unread,
Jun 24, 2014, 3:33:56 PM6/24/14
to django...@googlegroups.com
          for lis in Licenses.objects.values('code'):
             for post_value in insert_data:     
                 if lis in post_value:
                         license_tag += insert_data[lis] + ','

so im trying that now but the problem is django is causing issues when iterating over a dictionary i think because its a post dictionary 

Exception Value:
coercing to Unicode: need string or buffer, dict found
Exception Location:/home/grant/workspace/Django Projects/trunk/holon/portal/views.py in portal, line 171


insert_data
<QueryDict: {u'vm_id_posted': [u'27381'], u'vm_group_name_posted': [u'Unassigned'], u'vm_name_posted': [u'HyTrust-ESXi01'], u'assign_customer': [u'61'], u'vm_datacenter_id_posted': [u'2'], u'vm_edit': [u'edit'], u'vm_mor_posted': [u'vm-8026'], u'save': [u'save'], u'INGVMWESXI': [u'INGVMWESXI'], u'insert': [u'yes'], u'customer_id': [u'1'], u'csrfmiddlewaretoken': [u'4e8g52f3Hj4GmzWpQlfL8YvdJrETNZ8k'], u'vm_group_id_posted': [u'1'], u'vm_id': [u'27390'], u'vm_vcd_managed_posted': [u'Y'], u'vm_display_name_posted': [u'HyTrust-ESXi01']}>


post_value
u'vm_id_posted'

Roman Klesel

unread,
Jun 24, 2014, 4:07:07 PM6/24/14
to django...@googlegroups.com
Hello,

I would recommend to create a django form and feed the post data to this form.
Because 
1) the form will extract the post data for you
2) in the form you will be able the validate and clean the post data (you should definitively do that)

The code that saves the data to the form could also live in the form.


If the structure of the post data matches the structure of the database model closely, then a model form would be the easiest way.

Regards Roman


--
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...@googlegroups.com.
To post to this group, send email to django...@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/b4eb8f2f-e98a-437d-8656-1714f958af12%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages