create value to a model from another model's method - python

I'm trying to create a new table (Payslip model) that will contain the computed salary on an employee in a cutoff.
I want Payslip.salary to get value from Employee.compute_pay()
Given the example in the url link above, what should my views.py look like?
Is this the best approach to this kind of process? or Is there any library that can help with what I want to do here?
https://imgur.com/a/wVG5qrd
model.py
class Employee(models.Model):
name = models.CharField(max_length=20)
rate = models.IntegerField()
absent = models.IntegerField()
def __str__(self):
return self.name
def compute_pay(self):
daily = rate / 20
return rate - (daily*absent)
class Payslip(models.Model):
name = models.CharField(max_length=20)
salary = models.IntegerField()
def __str__(self):
return self.name
views.py
def compute(request):
if request.method == "POST":
return render(request,'payroll/compute/compute.html')

I do not think there is a need for another model Payslip, also you have no ForeignKey connections between the two models for it to work.
Considering your requirement, property decorator should work. Read up on how #property works. Basically, it acts as a pseudo model field, keep in mind the value of this field is not stored anywhere in the database, but it is tailor-made for situations like this where the field changes based on other fields and is needed for read-only.
Try this
class Employee(models.Model):
name = models.CharField(max_length=20)
rate = models.IntegerField()
absent = models.IntegerField()
def __str__(self):
return self.name
#property
def compute_pay(self):
daily = self.rate / 20
return (self.rate - (daily*self.absent))
you can get the employee's salary by Employee.compute_pay just like any other model field

Related

Django Model Occurrence Count

I'm fairly new to Django and I'm in need of assistance with my models.
class Region(models.Model):
region_name = models.CharField(max_length=10)
def __str__(self):
return self.region_name
class Property(models.Model):
prop_name = models.CharField(max_length=200)
region_name = models.ForeignKey(Region, on_delete=models.CASCADE, verbose_name="Region")
prop_code = models.IntegerField(default=0, verbose_name="Property")
def __str__(self):
return self.prop_name
class Sale(models.Model):
prop_name = models.ForeignKey(Property, on_delete=models.CASCADE)
employee = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="Person")
prop_state = models.CharField(null=True, max_length=5, choices=[('new','New'),('used','Used')])
date = models.DateField('Sale Date')
def __str__(self):
return '%s : %s %s - %s' % (self.prop_name.prop_name, self.employee, self.date, self.prop_state)
Here are my models. Property inherits from Region and Sale inherits from property. What I want to do is count the number of sales in a region and the number of sales on a specific property. However I do not know which would be the best way to approach this. I've tried using a lambda as a model field that uses the count() function but I wasn't able to see much success with that. Please let me know if you have any suggestions.
If you already have your Property/Region objects, something like this should work
sales_per_property = Sale.objects.filter(prop_name=property).count()
sales_per_region = Sale.objects.filter(prop_name__region_name=region).count()
Edit:
Seeing that you tried to add a lambda function to the model field, this may be more what you are looking for.
class Region(models.Model):
...
#property
def sales(self):
return Sale.objects.filter(prop_name__region_name=self).count()
and similarly for Property. Simply access the property using region.sales
You can annotate your querysets for Region and Property. For example:
from django.db.models import Count
regions = Region.objects.annotate(sales=Count('property__sale'))
properties = Property.objects.annotate(sales=Count('sale'))
The Regions/Propertys that arise from these querysets will have an extra attribute .sales that contains the number of related Sale objects.

How to manipulate value of one Model Field from another Model?

I have two models
class Employee(models.Model):
name = models.CharField(max_length=20)
ID = models.IntegerField()
basic_salary = models.IntegerField()
total_leave = models.IntegerField(default = 14)
paid_leave = models.IntegerField(default = 0)
unpaid_leave = models.IntegerField(default = 0)
def __str__(self):
return self.name
class Leave_management(models.Model):
name = models.OnetoOneField(Employee,on_delete= models.CASCADE)
reason = models.CharField(max_length=50)
from = models.DateTimeField()
to = models.DateTimeField()
total_days = models.IntegerField()
def __str__(self):
return self.name
So,i want to minus 'total_days' of 'model-Leave_management' from 'total_leave' field of 'model-Employee'. And as per leaves taken i want to update 'paid_leave' and 'unpaid_leave' sections.
I can perform so if these two models would be one model(example below), But i dont know how to perform so in different models.
def save(self,*args,**kwargs):
if self.total_days<=self.total_leave:
self.total_leave -= self.total_days
self.unpaid_leave = 14 - self.total_leave
else:
self.total_days -= 14
self.paid_leaves = self.total_days
super(Model_name,self).save(*args,**kwargs)
`
Please be guiding me.
In fact your OneToOneField(..) to an Employee is not a name. At the database level it will store values that correspond to primary keys of an Employee, and in Django, name will be a lazy fetch to the corresponding Employee. Therefore I suggest to rename your function to (for example) employee.
Another problem is that you define it as a OneToOneField. That means that an Employee has one Leave_management. But based on the fields (reason, from, to, etc.), it looks like an Employee can have zero, one, or more Leave_managements. So that means it is a ForeignKey.
So our model looks like:
class Leave_management(models.Model):
employee = models.ForeignKey(Employee,on_delete= models.CASCADE)
reason = models.CharField(max_length=50)
from = models.DateTimeField()
to = models.DateTimeField()
total_days = models.IntegerField()
def __str__(self):
return self.employee.name
Like the __str__ function already suggests, we can obtain the name of the employee by querying self.employee, and we can then fetch its .name attribute.
But now the challenge is what to do when save a Leave_management object. In that case the number of total_leave and paid_leave should be updated.
We first have to figure out the total number of total_days that are stored in Leave_management objects related to an Employee, this is equal to:
(Leave_management.objects.filter(employee=some_employee)
.aggregate(totals=Sum('total_days'))['totals'] or 0
So we can then subtract this from 14, and store the (possibly) remaining days in paid_leave, like:
class Leave_management(models.Model):
# ...
def save(self, *args, **kwargs):
super(Leave_management, self).save(*args, **kwargs)
totals = (Leave_management.objects
.filter(employee=some_employee)
.aggregate(totals=Sum('total_days'))['totals'] or 0
employee = self.employee
unpaid = min(14, totals)
employee.total_leave = 14 - unpaid
employee.unpaid_leave = unpaid
employee.paid_leave = totals - unpaid
employee.save()
Note: typically we do not handle this by overriding the .save(..) function, but by using Django signals: triggers that can be implemented when certain objects are saved, etc. This especially should be used since the objects can be changed circumventing the .save(..) function, and sometimes such objects might get deleted as well. So the above is not a good design decision.
Even when we use signals, it is a good idea to frequently (for example once a day) recalculate the total leave, and update the corresponding Employee models.

Django - default field value depends on other field value

I have a problem to set a default field value. What I want to do?I want that price in class Packages be a default value of priceNoTax in class Bill. As you can see, all three classes are
logically connected.Example: Account 1 has a package with id 1. Price of this package is 100. Default value of priceNoTax for Account 1 is 100.How to do that? I am relative new at this, so I need help.
models.py
class Packages(models.Model):
#other fields
price = models.IntegerField(validators=[MinValueValidator(1)], verbose_name="Price of package")
class Account(models.Model):
startDate = models.DateField(verbose_name="Start date")
finishDate = models.DateField(verbose_name="Finish date")
idPackage = models.ForeignKey(Packages, on_delete=models.CASCADE, verbose_name="Package")
class Bill(models.Model):
date = models.DateField(default=datetime.now())
tax = models.FloatField(default=0.20)
priceNoTax = models.IntegerField()
priceTax = models.FloatField(default=priceNoTax+(priceNoTax*tax))
idAccount = models.ForeignKey(Account, on_delete=models.CASCADE, verbose_name="Account")
def __str__(self):
return self.date
Thanks a lot!!!
perhaps add this to your Bill class?
def save(self, *args, **kwargs):
if self.priceNoTax is None:
self.priceNoTax = self.idAccount.idPackage.price
super(Bill, self).save(*args, **kwargs)
Why do you need it to be a field? Do you see a reason where someone would want to change the total price without changing the price and tax to the corresponding values? If it doesn't really need to be a field, you can just make it a method.
class Bill(models.Model):
date = models.DateField(default=datetime.now())
tax = models.FloatField(default=0.20)
priceNoTax = models.IntegerField()
idAccount = models.ForeignKey(Account, on_delete=models.CASCADE, verbose_name="Account")
def priceTax(self):
return self.priceNoTax + (self.priceNoTax*self.tax)
def __str__(self):
return self.date
You can still use it the same way in templates with {{ bill.priceTax }}. In code you would need to use bill.priceTax().
This way, the price with tax should stay up-to-date no matter how the tax or price without tax changes.
You can also use #property decorator to avoid having to call it as a function in code.
#property
def priceTax(self):
return self.priceNoTax + (self.priceNoTax*self.tax)
For more see https://docs.djangoproject.com/en/2.0/topics/db/models/#model-methods
#kichik answered how to display the default value using template tag, and I recommend to implement auto-calculation by javascript.
Of course, you will have to validate user input or implement save() method as #Chris Curvey said.

Django model relationship - Fitness App

Currently my models are:
class Workout(models.Model):
date = models.DateField()
routine = models.ForeignKey('Routine')
def __str__(self):
return '%s' % self.date
class Routine(models.Model):
name = models.CharField(max_length=255)
exercises = models.ManyToManyField('Exercise')
def __str__(self):
return self.name
class Exercise(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
I want the user to be able to create a new entry specified by a date(Workout). They can also create routines(Routine), associated with the date and filled with different exercises(Exercise) which they can also create.
Here is the part I can't figure out.
I want the user, when adding a new exercise, to be able to choose whether it is a strength exercise or cardio exercise. Strength exercises will have fields like: #of sets, reps, and weight. Where as carido will have fields like length and speed.
I am unclear on how to relate the two types of exercises to the Exercise class.
The most common way of doing this, is to create a generic relationship, such as:
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class Exercise(models.Model):
name = models.CharField(max_length=255)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
info = GenericForeignKey('content_type', 'object_id')
def __str__(self):
return self.name
class StrengthExercise(models.Model):
sets, reps, weight = (...)
class CardioExercise(models.Model):
length, speed = (...)
Example use:
>>> from app_name.models import Exercise, CardioExercise
>>> exercise_info = CardioExercise.objects.create(length=600, speed=50)
>>> exercise = Exercise(name="cardio_exercise_1", info=exercise_info)
>>> exercise.save()
>>> exercise.info.length
600
>>> exercise.info.__class__.__name__
'CardioExercise'
OBS: Make sure you have 'django.contrib.contenttypes' in your INSTALLED_APPS (enabled by default).

Adding dynamic fields to Django models

How do I create a dynamic field on a model?
Let's say I'm writing an application related to the stock market. I make a purchase on one day and sometime later I want to check the gain (or loss) based on today's price. I'd have a model like this:
class Purchase(models.Model):
ticker = models.CharField(max_length=5)
date = models.DateField()
price = models.DecimalField(max_digits=20, decimal_places=3)
quantity = models.IntegerField()
What I'd like to do is define a model something like this:
class PurchaseGain(Purchase):
gain = models.DecimalField(max_digits=20, decimal_places=3)
class Meta:
proxy = True
So that I could do this:
todays_price = get_price_from_webservice(ticker)
for p in PurchaseGain.objects.get_purchase_gain(todays_price):
print '%s bought on %s for a gain of %s' % (p.ticker, p.date, p.gain)
Where p.gain is dynamically computed based on the input to get_purchase_gain. Rather than just constructing dictionaries on the fly I want to use a model, because I'd like to pass this around and generate forms, save changes, etc from the instance.
I tried creating a derived QuerySet, but that led to a circular dependency, because Purchase needed to know about the QuerySet (through a custom manager) and the QuerySet returned an iterator that needed to instantiate a PurchaseGain, which was derived from Purchase.
What options do I have?
Thanks,
Craig
Why not add a gain() method to your model?
class Purchase(models.Model):
ticker = models.CharField(max_length=5)
date = models.DateField()
price = models.DecimalField(max_digits=20, decimal_places=3)
quantity = models.IntegerField()
def gain(self, todays_price=None):
if not todays_price:
todays_price = get_price_from_webservice(self.ticker)
result_gain = todays_price - self.price
return result_gain
Then you can pretty much do what you want:
for p in Purchase.objects.all():
print '%s bought on %s for a gain of %s' % (p.ticker, p.date, p.gain())
Creating a proxy class is what confused me. By just adding attributes to a Purchase, I was able to accomplish what I wanted.
class PurchaseQuerySet(QuerySet):
def __init__(self, *args, **kwargs):
super(PurchaseQuerySet, self).__init__(*args, **kwargs)
self.todays_price = None
def get_with_todays_price(self, todays_price):
self.todays_price = todays_price
cloned = self.all()
cloned.todays_price = todays_price
return cloned
def iterator(self):
for p in super(PurchaseQuerySet, self).iterator():
p.todays_price = self.todays_price
yield p
class PurchaseManager(models.Manager):
def get_query_set(self):
return PurchaseQuerySet(self.model)
def __getattr__(self, name)
return getattr(self.get_query_set(), name)
class Purchase(models.Model):
ticker = models.CharField(max_length=5)
date = models.DateField()
price = models.DecimalField(max_digits=20, decimal_places=3)
quantity = models.IntegerField()
objects = PurchaseManager()
#property
def gain(self):
return self.todays_price - self.price
Now I can do:
for p in Purchase.objects.filter(ticker=ticker).get_with_todays_price(100):
print p
print p.gain

Categories