Martini Siu
Martini Siu

Reputation: 23

How to initialise a field in Django models using other fields in the model

I would like to call an API that will return the latitude and longitude from a postcode which is part of my Django Model

class Nurse(models.Model):
    firstName = models.CharField(max_length=100, default="")
    lastName = models.CharField(max_length=100, default="")
    postCode = models.CharField(max_length=10, default="")
    area = models.CharField(max_length=100)
    phoneNumber = models.CharField(max_length=20)
    email = models.EmailField(max_length=100, null=True)
    latitude = models.FloatField(max_length=10, null=True)
    longitude = models.FloatField(max_length=10, null=True)

So when a nurse object is created, I would like to call an api (postcode.io) which means I am able to get a latitude and longitude based on the postcode that saves these values to the data model. I need to have the latitude and longitude because I have use for it in a template where I need to access this information.

Thanks!

Upvotes: 0

Views: 124

Answers (3)

Martini Siu
Martini Siu

Reputation: 23

The best way I have found is to use @James Bond's method for the following reason.

class Nurse(models.Model):

    def save(self, *args, **kwargs):
        self.longitude, self.lattitude = shadyAPI.giveAwayStrangersPersonalData(self.email, self.firstname, self.lastname, self.postcode, self.area, self.phoneNumber, get_user_model().objects.get(nursePK=self).password)
        super().save(self, *args, **kwargs)

I am only an okay developer but here are my reason incase this could help you.

This method means that every time I update the nurse object ie. Change via a form or when I initially add a new instance of a nurse, it will automatically complete the required fields which is great. I can see however, that it could be bad because it calls the api every time I update the instance which could incur errors but for my use, this is fine and a great solution.

I hope this helps :)

Upvotes: 0

James Bond
James Bond

Reputation: 379

You can do it in save() if it will change or init() if this is supposed to only happen once.

class Nurse(models.Model):

    #Variant 1
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.longitude, self.lattitude = shadyAPI.giveAwayStrangersPersonalData(self.email, self.firstname, self.lastname, self.postcode, self.area, self.phoneNumber, get_user_model().objects.get(nursePK=self).password)

    #Variant 2
    def save(self, *args, **kwargs):
        self.longitude, self.lattitude = shadyAPI.giveAwayStrangersPersonalData(self.email, self.firstname, self.lastname, self.postcode, self.area, self.phoneNumber, get_user_model().objects.get(nursePK=self).password)
        super().save(self, *args, **kwargs)

Upvotes: 0

asad_hussain
asad_hussain

Reputation: 2011

You can achieve this by defining model methods.

class Nurse(models.Model):
    firstName = models.CharField(max_length=100, default="")
    lastName = models.CharField(max_length=100, default="")
    postCode = models.CharField(max_length=10, default="")
    area = models.CharField(max_length=100)
    phoneNumber = models.CharField(max_length=20)
    email = models.EmailField(max_length=100, null=True)
    latitude = models.FloatField(max_length=10, null=True)
    longitude = models.FloatField(max_length=10, null=True)

def latitude(self):
   # here you can access the postCode of this model object using self.postCode

def longitude(self):
   # same can be done here

Upvotes: 1

Related Questions