I'm currently learning the Django framework, and I have a Account
model. I want to retrieve data from an external REST API to populate the fields of this model. Here's an example:
class Account(models.Model):
external_id = models.CharField(max_length=60)
account_id = models.CharField(max_length=60)
puuid = models.CharField(max_length=85)
name = models.CharField(max_length=20)
def __str__(self) -> str:
return "{} (id: {})".format(self.name, self.external_id)
@classmethod
def create(cls, nickname):
# Here I would like to call the external API in order to recover some of those fields.
account = cls(external_id = '', account_id = '', puuid = '', name = nickname)
return account
I can simply import the requests
library here, make API calls, and it will work. However, I would like to know the correct way to do it. I assume that I should create a services.py
file and implement the API call logic there, but I'm unable to find a style guide. Is there any convention?