Use CharField as a string
<p>I want to create a model where I just put in two fields and the rest is generated with API requests, the generation I've got but I can't get the CharField in a usable string format.
So on field is:</p>

<pre><code>name = models.CharField(max_length=250, default='default')
</code></pre>

<p>And I want to run it through:</p>

<pre><code>name = self.name.replace(' ', '+')
movieData = (json.loads(str(urllib2.urlopen('http://www.omdbapi.com/?t=' + name + '&amp;y=&amp;plot=short&amp;r=json').read(),'utf-8')))
movieYear = movieData.get('Year')
</code></pre>

<p>Saving movieYear as a field.
Thanks in advance</p>

<p>I think a custom <code>save</code> will help you accomplish what you are looking to do. Here's an example:</p>

<pre><code>class MyModel(models.Model):
    name = models.CharField(max_length=250, default='default')
    movieData = models.JSONField(blank=True)
    movieYear = models.CharField(max_length=250, blank=True)

    def save(self, *args, **kwargs):
        url = 'http://www.omdbapi.com/?t={}&amp;y=&amp;plot=short&amp;r=json'.format(self.name.replace(' ', '+'))
        data = json.loads(str(urllib2.urlopen(url).read(),'utf-8'))
        self.movieData = data 
        self.movieYear = data['Year']
        super(MyModel, self).save(*args, **kwargs)
</code></pre>

<p>This will let you make a request to a URL crafted from the <code>self.name</code> attribute (replacing spaces with <code>+</code>), then store the response in the fields you wanted. </p>

<p>If you use this solution, you will want to take care to include some kind of error handling - what happens if an invalid name is supplied? Or the returned data doesn't have a <code>Year</code> key for some reason? Or maybe the API is just down for a few minutes, so your valid request receives no response? </p>

<p>This will also make a request to that API on every save, which may have consequences for the speed of your application. You can force logic in a custom <code>save</code> to execute only when creating a new instance by using <code>if self.pk is None</code>.</p>

python json django web