Re: JSON file

2020-05-25 Thread Derek
I am not sure about the field names part, but you could probably create 
that manually very easily.

For the data section, you need a list of lists, which you can get from code 
like:

import json
data_ready_for_json = 
list(mail_item_count_deliveries_perDate.objects.values_list('countDeliveries','Dates'))
json_string = json.dumps(data_ready_for_json)

(P.S. you may want to consider giving your classes more Python-like names 
e.g. class MailDelivery )


On Friday, 22 May 2020 19:48:36 UTC+2, HJ wrote:
>
> hello django users hope you are doing well 
>
> I want to do a highchart using a json file , and that's the problem I am 
> facing , I want to create a json file in my views.py based on my columns 
> below "countDeliveries"and "Dates"
>
> class mail_item_count_deliveries_perDate(models.Model):
>countDeliveries = models.IntegerField(default=0)
>Dates = models.DateTimeField()
>
>
> myJSON file should be something like that:
>  
>
> [
> [
> Dates,
> countDeliveries
> ],
> [
> 116769600,
> 5
> ],
>
>
>  
> - can you guys help me out 
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/19830969-d813-4e51-9016-b9b820945b92%40googlegroups.com.


RE: JSON file

2020-05-22 Thread Vishesh Mangla
I ‘m not sure if that is a JSON file because a JSON file is nothing but a python dictionary like structure and that doesn’t seem to be that. Anyways you can see python’s building json module’s json.dumps() function. Sent from Mail for Windows 10 From: HJSent: 22 May 2020 23:18To: Django usersSubject: JSON file hello django users hope you are doing well  I want to do a highchart using a json file , and that's the problem I am facing , I want to create a json file in my views.py based on my columns below "countDeliveries"and "Dates" class mail_item_count_deliveries_perDate(models.Model):    countDeliveries = models.IntegerField(default=0)    Dates = models.DateTimeField() myJSON file should be something like that: [        [                Dates,                countDeliveries        ],        [                116769600,                5        ], - can you guys help me out -- 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+unsubscr...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/65341e88-88b1-480f-9d9a-39ce909ee4da%40googlegroups.com. 



-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5ec81901.1c69fb81.33bd5.fa47%40mx.google.com.


JSON file

2020-05-22 Thread HJ
hello django users hope you are doing well 

I want to do a highchart using a json file , and that's the problem I am 
facing , I want to create a json file in my views.py based on my columns 
below "countDeliveries"and "Dates"

class mail_item_count_deliveries_perDate(models.Model):
   countDeliveries = models.IntegerField(default=0)
   Dates = models.DateTimeField()


myJSON file should be something like that:
 

[
[
Dates,
countDeliveries
],
[
116769600,
5
],


 
- can you guys help me out 

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/65341e88-88b1-480f-9d9a-39ce909ee4da%40googlegroups.com.


Re: HTML form filled, proposes a download link for the data filled in a json file

2019-04-09 Thread Robin Riis
Hello!

well in the form is there anything that will be saved in a database?
if so you can start by looking at models.py in your application.

if not you can jump directly to forms.py.

an example of a model and form:

*** in models.py
from django.db import models

class Company(models.Model):
"""This is your database model with the fields you want to store and
fetch!
"""
name = models.CharField(max_length=60)
telephone = models.CharField(max_length=15, null=True, blank=True)
info = models.TextField(blank=True, null=True)
is_awesome = models.BooleanField(default=False)

*** in forms.py
from django import forms
from .models import Company

class CompanyForm(forms.ModelForm):
class Meta:
model = Company
fields = '__all__'
widgets = {
'is_awesome': forms.HiddenInput(),
}

then when you have a form and model you can use the generic views like this:

*** in views.py
from django.urls import reverse_lazy
from django.views.generic import CreateView
from django.contrib.messages.views import SuccessMessageMixin

from .models import Company
from .forms import CompanyForm

class Add_Company(SuccessMessageMixin, CreateView):
template_name = 'company/add_a_new.html'
success_message = 'a new company was added!'
success_url = reverse_lazy('home')
form_class = CompanyForm

thats it!


Den tis 9 apr. 2019 kl 00:20 skrev Bassem Boulfarkat <
bassem.boulfar...@gmail.com>:

> I am working on creating a website based on the django framework with
> python. The aim is to have a simple HTML form that will build around where
> the user fills in the form and when it is done can submit it. When
> submitted, he will have a json file downloaded.
>
> After looking around, I found that serializer would be the best way but I
> am still really confused. Would appreciate any kind of help.
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/3bd76a23-e5d8-4597-8fc1-3e9f2fc7cb90%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/3bd76a23-e5d8-4597-8fc1-3e9f2fc7cb90%40googlegroups.com?utm_medium=email_source=footer>
> .
> 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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPLZMbNnCxVTa%2BXkpGHYSrwYjYPMyvfhtrsTMpkY0E_dxvT16A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


HTML form filled, proposes a download link for the data filled in a json file

2019-04-08 Thread Bassem Boulfarkat


I am working on creating a website based on the django framework with 
python. The aim is to have a simple HTML form that will build around where 
the user fills in the form and when it is done can submit it. When 
submitted, he will have a json file downloaded.

After looking around, I found that serializer would be the best way but I 
am still really confused. Would appreciate any kind of help.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3bd76a23-e5d8-4597-8fc1-3e9f2fc7cb90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Filling out a form using a json file

2016-12-13 Thread jochen . luig
Hello Abraham,

thanks for your answer!

On Mon, 12. Dec 10:15, 'Abraham Varricatt' via Django users 
<django-users@googlegroups.com> wrote:
> What about using the forms validate() method? i.e. if the user has not 
> filled in all the JSON details, return a validation error. This should give 
> them the opportunity to make edits or re-type as needed.

Yes, I probably should validate the input that way, but what I'm
concerned with at the moment is how the JSON details make it to the
form object in the first place.
I probably got overwhelmed reading the API Docs and thought there has
to be a more elegant way to handle the JSON -> Form Object part than
just writing a ``post`` method.
 
> > On another note, there are several Models I want to work with in 
> > this way so it would be even better if I could select the model 
> > class according to the contents of the json file. 
> >
> 
> This sounds like a validation job to me. Just put in all the logic checks 
> into the validate() method. Bear in mind that you are likely to receive bad 
> input, so handle accordingly. 

As in validating the ModelForms for each object and choose the one
that validates successfully if any?

Jochen

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/20161213081852.GA12521%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Filling out a form using a json file

2016-12-12 Thread 'Abraham Varricatt' via Django users
Hello Jochen,

On Monday, December 12, 2016 at 9:17:11 AM UTC-5, joche...@gmail.com wrote:
>
>
> for editing Objects, but it seems that those views can only deal with 
> objects that already are in the database.  So is there a more 
> idiomatic way to solve this or am I stuck with the above solution? 
>

What about using the forms validate() method? i.e. if the user has not 
filled in all the JSON details, return a validation error. This should give 
them the opportunity to make edits or re-type as needed.

 

> On another note, there are several Models I want to work with in 
> this way so it would be even better if I could select the model 
> class according to the contents of the json file. 
>

This sounds like a validation job to me. Just put in all the logic checks 
into the validate() method. Bear in mind that you are likely to receive bad 
input, so handle accordingly. 

Yours sincerely,
Abraham V.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ed0b8b47-9922-45e1-9899-745ff6197275%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Filling out a form using a json file

2016-12-12 Thread jochen . luig
Hi,

I have a json file describing an object that I want to import into my
app. For that purpose, I want to upload it using a form field and fill
a ModelForm so the contents can be checked and edited by a user before
saving it to the database.  This is what I'm currently doing:

# in forms.py
class MyObjectRequestForm(forms.ModelForm):
"""
Form for generating a MyObject from a json file
"""
class Meta:
model = MyObject
exclude = ()

# in views.py
class MyObjectFromFile(LoginRequiredMixin, View):

def post(self, request):
"""
generate Form from json in 'message_file' parameter.
"""
context = dict()
received_object = json.load(request.FILES['message_file'])
new_my_object = MyObject()
new_my_object.derive_from_message(received_object)
context['form'] = MyObjectRequestForm(model_to_dict(new_my_object))
return render(request, 'myapp/my_object_form.html', context)

I already have a CreateView and UpdateView for this kind of object so
I probably should inherit from one of those to take advantage of the
``get_success_url`` method and to avoid writing a completely new view
for editing Objects, but it seems that those views can only deal with
objects that already are in the database.  So is there a more
idiomatic way to solve this or am I stuck with the above solution?
On another note, there are several Models I want to work with in
this way so it would be even better if I could select the model
class according to the contents of the json file.

Kind regards,

Jochen

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/20161212125441.GC26289%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


How to read the json file of a dinamical way in relation to their size structure

2016-10-10 Thread Bernardo Garcia


I have the following JSON file named ProcessedMetrics.json which is 
necessary read for send their values to some template:


 {
  "paciente": {
"id": 1234,
"nombre": "Pablo Andrés Agudelo Marenco",
"sesion": {
  "id": 12345,
  "juego": [
{
  "nombre": "bonzo",
  "nivel": [
{
  "id": 1234,
  "nombre": "caida libre",
  "segmento": [
{
  "id": 12345,
  "nombre": "Hombro",
  "movimiento": [
{
  "id": 1234,
  "nombre": "flexion",
  "metricas": [
{
  "min": 12,
  "max": 34,
  "media": 23,
  "moda": 20
}
  ]
}
  ]
}
  ],
  "___léeme___": "El array 'iteraciones' contiene las vitorias o 
derrotas con el tiempo en segundos de cada iteración",
  "iteraciones": [
{
  "victoria": true,
  "tiempo": 120
},
{
  "victoria": false,
  "tiempo": 232
}
  ]
}
  ]
}
  ]
}
  }}



Through of the following class based view I am reading a JSON file:

class RehabilitationSessionDetail(LoginRequiredMixin,DetailView):
model = RehabilitationSession
template_name = 'rehabilitationsession_detail.html'

def get_context_data(self, **kwargs):
context=super(RehabilitationSessionDetail, 
self).get_context_data(**kwargs)
is_auth=False

user = self.request.user
if user.is_authenticated():
is_auth=True

with open('ProcessedMetrics.json') as data_file:
session_data=json.loads(data_file.read())

#Sending a data to template
   context.update({'is_auth':is_auth,
   'session_data':session_data
 })
   return context


In my template rehabilitationsession_detail.html I put my tag of this way:

{{session_data.paciente.sesion.juego}} 

Then I get the document json in my template:

<http://i.stack.imgur.com/SLZB2.png>


In my template, I want get the dictionary(before json document) values of a 
separate way such as follow:


<http://i.stack.imgur.com/MsyHT.png>







The idea is that without matter the nested levels of the json document I 
can get the values. 


Sometimes, the json document will have more identation levels in their 
structure and other times will be a json document more simple


I would that independently of the json document size (if this have more 
than one item in your arrays) will be possible read and get all the values.

I try accessing to the specific item from the RehabilitationSessionDetail view 
of this way:


segment = 
data["paciente"]["sesion"]["juego"][0]["nivel"][0]["segmento"][0]["nombre"]


And this works, but not always I will get the same json document structure.

In summary, How to can I get the values (nested and parents) of my json 
document for send them to the template?

I hope can be clear in my question. Any orientation is highly graceful



-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c5256c63-03e3-42ff-9e05-41b181292ee4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Creating JSON file

2013-04-08 Thread Seth Gordon
You want to output a list of one dict per object, and you want that dict to
contain another dict inside it. So you don’t need dict2. Instead

f = file('report.json','a+')
sys.stdout = f
result = []
objects = game_objects.objects.all()
for obj in objects:
time = time_stamp.objects.filter(user_id = obj.user_id)
obj_id = obj.object_id
per = performance.objects.filter(user_id = obj.user_id)
pat = pattern.objects.filter(user_id = obj.user_id)
dict1 = {'user_id' : obj.user_id, 'game_id': obj.game_id}
dict1[obj_id]={'color': obj.color, 'speed': obj.speed, 'energy':
obj.energy }
result.append(dict1)


On Mon, Apr 8, 2013 at 8:33 AM, Nagarajan Dharmar Sitha <
nagarajan.dharmarsi...@gmail.com> wrote:

> saved = sys.stdout
> f = file('report.json','a+')
> sys.stdout = f
> result = []
> objects = game_objects.objects.all()
> for obj in objects:
> dict2 ={}
> time = time_stamp.objects.filter(user_id = obj.user_id)
> obj_id = obj.object_id
> per = performance.objects.filter(user_id = obj.user_id)
> pat = pattern.objects.filter(user_id = obj.user_id)
> dict2[obj_id]={'color': obj.color, 'speed': obj.speed, 'energy':
> obj.energy }
> dict1 = {'user_id' : obj.user_id, 'game_id': obj.game_id}
> result.append(dict1)
> result.append(dict2)
>
> print json.dumps(result, indent =  4)
> sys.stdout = saved
>
> this is my code and out put of the code is:
>
>
>
> [
> {
> "game_id": "000g1",
> "user_id": "000u1"
> },
> {
> "000o1": {
> "color": "red",
> "energy": "300",
> "speed": 2
> }
> },
> {
> "game_id": "000g2",
> "user_id": "000u2"
> },
> {
> "000o2": {
> "color": "red",
> "energy": "450",
> "speed": 6
> }
> },
> {
> "game_id": "000g3",
> "user_id": "000u3"
> },
> {
> "000o3": {
> "color": "red",
> "energy": "600",
> "speed": 12
> }
> },
> {
> "game_id": "000g4",
> "user_id": "000u4"
> },
> {
> "000o4": {
> "color": "red",
> "energy": "750",
> "speed": 20
> }
> },
> {
> "game_id": "000g5",
> "user_id": "000u5"
> },
> {
> "000o5": {
> "color": "red",
> "energy": "900",
> "speed": 30
> }
> },
> {
> "game_id": "000g6",
> "user_id": "000u6"
> },
> {
> "000o6": {
> "color": "red",
> "energy": "1050",
> "speed": 42
> }
> },
> {
> "game_id": "000g7",
> "user_id": "000u7"
> },
> {
> "000o7": {
> "color": "red",
> "energy": "1200",
> "speed": 56
> }
> },
> {
> "game_id": "000g8",
> "user_id": "000u8"
> },
> {
> "000o8": {
> "color": "red",
> "energy": "1350",
> "speed": 72
> }
> },
> {
> "game_id": "000g9",
> "user_id": "000u9"
> },
> {
> "000o9": {
> "color": "red",
> "energy": "1500",
> "speed": 90
> }
> }
> ]
>
>
> but i want out put like this way:
>
>
> [
> {
> "game_id": "000g1",
> "user_id": "000u1"
> "000o1": {
> "color": "red",
> "energy": "300",
> "speed": 2
> }
> },
> {
> "game_id": "000g2",
> "user_id": "000u2",
> "000o2": {
> "color": "red",
> "energy": "450",
> "speed": 6
> }
> },
> ..
> .
> ]
>
>
> kindly help me out...
>
>  --
> 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+unsubscr...@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?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
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+unsubscr...@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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Creating JSON file

2013-04-08 Thread Nagarajan Dharmar Sitha
saved = sys.stdout
f = file('report.json','a+')
sys.stdout = f
result = []
objects = game_objects.objects.all()
for obj in objects:
dict2 ={}
time = time_stamp.objects.filter(user_id = obj.user_id)
obj_id = obj.object_id
per = performance.objects.filter(user_id = obj.user_id)
pat = pattern.objects.filter(user_id = obj.user_id)
dict2[obj_id]={'color': obj.color, 'speed': obj.speed, 'energy': 
obj.energy }
dict1 = {'user_id' : obj.user_id, 'game_id': obj.game_id}
result.append(dict1)
result.append(dict2)

print json.dumps(result, indent =  4)
sys.stdout = saved

this is my code and out put of the code is:



[
{
"game_id": "000g1",
"user_id": "000u1"
},
{
"000o1": {
"color": "red",
"energy": "300",
"speed": 2
}
},
{
"game_id": "000g2",
"user_id": "000u2"
},
{
"000o2": {
"color": "red",
"energy": "450",
"speed": 6
}
},
{
"game_id": "000g3",
"user_id": "000u3"
},
{
"000o3": {
"color": "red",
"energy": "600",
"speed": 12
}
},
{
"game_id": "000g4",
"user_id": "000u4"
},
{
"000o4": {
"color": "red",
"energy": "750",
"speed": 20
}
},
{
"game_id": "000g5",
"user_id": "000u5"
},
{
"000o5": {
"color": "red",
"energy": "900",
"speed": 30
}
},
{
"game_id": "000g6",
"user_id": "000u6"
},
{
"000o6": {
"color": "red",
"energy": "1050",
"speed": 42
}
},
{
"game_id": "000g7",
"user_id": "000u7"
},
{
"000o7": {
"color": "red",
"energy": "1200",
"speed": 56
}
},
{
"game_id": "000g8",
"user_id": "000u8"
},
{
"000o8": {
"color": "red",
"energy": "1350",
"speed": 72
}
},
{
"game_id": "000g9",
"user_id": "000u9"
},
{
"000o9": {
"color": "red",
"energy": "1500",
"speed": 90
}
}
]


but i want out put like this way:


[
{
"game_id": "000g1",
"user_id": "000u1"
"000o1": {
"color": "red",
"energy": "300",
"speed": 2
}
},
{
"game_id": "000g2",
"user_id": "000u2",
"000o2": {
"color": "red",
"energy": "450",
"speed": 6
}
},
..
.
]


kindly help me out...

-- 
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+unsubscr...@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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.