> Den 2. maj 2016 kl. 15.38 skrev Shameema Mohsin <[email protected]>:
>
> Could anybody help me out as I am new to Django.
> In my model:
>
> from django.db import models
>
> class Employees(models.Model):
> empId = models.AutoField(primary_key=True)
> empName = models.CharField(max_length=200)
> empEmail = models.EmailField()
> empMob = models.CharField(max_length=200)
> empAddr = models.TextField()
> empLat = models.DecimalField(max_digits=9,decimal_places=6)
> empLong = models.DecimalField(max_digits=9,decimal_places=6)
> empDOB = models.DateField()
> zval = models.IntegerField(default=0)
>
> def __str__(self):
> return self.empName
>
> In the z value field i need to insert the z curve value calculated from
> latitude and longitude. where should I write the code? In models.py or
> somewhere else?
There are two strategies here. If zval is always generated from empLat and
empLong, then you could change zval from a field to a property on the Employees
class. Otherwise, you risk zval falling out of sync if you are not careful to
always re-calculate zval when empLat/empLong changes:
class Employees(models.Model):
[...]
@property
def zval(self):
return # your calculation here
The other strategy would be to place the calculation in the save() method:
class Employees(models.Model):
[...]
def save(self, *args, **kwargs):
self.zval = # your calculation here
super().save(*args, **kwargs)
Beware that save() is not always called. For example, bulk_create() doesn't
call save(). You need to handle these situations yourself.
Erik
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/97FEF314-509F-48AF-8A41-860CDBC6F514%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.