mardi 12 avril 2016

Update a database with external API data in django

I'm learning Django for a project I'm working on and I'm having a problem implementing this part, basically I want my this to happen:

  1. The user enters a term into a search box
  2. The server queries a public API and adds some of the data to the database

I have my model set up with a form in the view. So I can get the search term from the post request. However I'm unsure how to then use that to gather the data and save it to the database.

The model is just a list of basic fields:

fileName = models.CharField(max_length=120)
rating = models.FloatField()
date = models.DateField()

The form is just a text box for the fileName:

class searchFile(forms.ModelForm):
class Meta:
    model = file
    fields = ['fileName']

I can work out how to gather the data from the API myself, I'm more confused about how to input data to the database from the code rather than directly from the form.

For instance how could I make it so that when a user inputs a fileName into the form, fileName is input into the database and all the other fields are filled with random data?




Aucun commentaire:

Enregistrer un commentaire