B
B
blackbb2017-04-30 10:18:05
Django
blackbb, 2017-04-30 10:18:05

How to write data to json when object is created?

There is a model:

class School(models.Model):
    slug = models.SlugField(unique=True, verbose_name='ЧПУ')
    title = models.CharField(max_length=300, verbose_name='Название')
    adress = models.CharField(max_length=300, verbose_name='Адрес')
    def __unicode__(self):
        return u'%s' % (self.title)
        
    def mygeo(self):
        g = geocoder.google(self.adress)
        return g.latlng

The task when creating an object is to write data to data.json. Now it looks like this:
{
    "type": "FeatureCollection",
    "features": [
        {"type": "Feature", "id": 0, "geometry": {"type": "Point", "coordinates": [55.831903, 37.411961]}, "properties": {"balloonContentHeader": "<font size=3><b><a target='_blank' href='https://yandex.ru'>Здесь может быть ваша ссылка</a></b></font>"}}
       
    ]
}

Actually you need to do this:
{
    "type": "FeatureCollection",
    "features": [
        {"type": "Feature", "id": "{{school.id}}", "geometry": {"type": "Point", "coordinates": {{school.mygeo}}}, "properties": {"balloonContentHeader": "<font size=3><b><a target='_blank' href='/school/{{school.slug}}'>{{school.title}}</a></b></font>"}}
       
    ]
}

Please help me implement writing to json format when creating School, please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2017-04-30
@dimonchik2013

json.loads()
json.dumps()

what didn't you like?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question