Updata data based on the status django model - python

I am currently creating a model and one of the field is the percentage. I wanted to update the percentage field depending on the status.
this is my models.py
class Contact(models.Model):
STATUS = (
('NY','Not Yet'),
('RN','Running'),
('CO','Completed'),
)
status = models.CharField(max_length=2, choices=STATUS, default='NY')
percentage = models.FloatField()
if status == 'CO':
percentage = 100
else:
percentage = 0
and this does not work or does not save.

If you're just using the percentage field for display purposes, you don't need to make it a real field. I would do it like this:
class Contact(models.Model):
STATUS = (
('NY','Not Yet'),
('RN','Running'),
('CO','Completed'),
)
status = models.CharField(max_length=2, choices=STATUS, default='NY')
#property
def percentage(self):
if self.status == 'CO':
return 100
else:
return 0
But if you really do want it to be a field, you could do something like what Kapil Sachdev's answer has.

Override Model's save method and put your logic in there.
class Contact(models.Model):
STATUS = (
('NY','Not Yet'),
('RN','Running'),
('CO','Completed'),
)
status = models.CharField(max_length=2, choices=STATUS, default='NY')
percentage = models.FloatField()
def save(self, *args, **kwargs):
if self.status == 'CO':
self.percentage = 100
else:
self.percentage = 0
super(Contact, self).save(*args, **kwargs) # Call the "real" save() method.

Related

How to get total quantity of each kind of task in Django?

I have 1 table for tasks, those tasks can be in 3 status"todo","in-progress" and "done", I
want to calculate total number of each status' task, and put it into an array like ('todo
total','progress total','done total'), any idea how can I achieve that? my final goal is to
display the 3 subtotal in Chartjs, Thanks in advance.
models.py
'''
class Todo(models.Model):
status_option = (
('to_do', 'to_do'),
('in_progress', 'in_progress'),
('done', 'done'),
)
status = models.CharField(max_length=20, choices=status_option, default='to_do')
# todo_list's content
team = models.ForeignKey('Team', on_delete=models.CASCADE)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
name = models.CharField(max_length=20)
create_date = models.DateTimeField(auto_now_add=True)
start_date = models.DateTimeField(default=datetime.datetime.now)
due_date = models.DateTimeField(default=datetime.datetime.now)
project_code = models.CharField(max_length=20)
details = models.TextField()
def __str__(self):
return self.status
# return self.team['team'].queryset
def update_status(self):
if self.status == 'to_do':
self.status = 'in_progress'
elif self.status == 'in_progress':
self.status = 'done'
self.save()
'''
If you want to count one status by itself you can do this:
to_do_count = Todo.objects.filter(status='to_do').count()
If you want a dictionary counting each status you can do this:
from django.db.models import Case, When
counts_data = Todo.objects.aggregate(
to_do_count=Count(Case(When(status='to_do', then=1))),
in_progress_count=Count(Case(When(status='in_progress', then=1))),
done_count=Count(Case(When(status='done', then=1))),
)
or this:
from django.db.models import Q
counts_data = Todo.objects.aggregate(
to_do_count = Count('pk', filter=Q(status='to_do')),
in_progress_count = Count('pk', filter=Q(status='in_progress')),
done_count = Count('pk', filter=Q(status='done'))
)

How to auto-generate value from another entity on the same table

I have a table that has name and amount entity, and I want to happen is:
this is the sample of my table on models.py:
class Test(models.Model):
test_id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
amount = models.IntegerField()
timestamp = models.DateTimeField()
if entered amount is 300, the name will be 'hello', else if the entered amount is 500, the name will be 'world'. please help, been trying to figure this out since yesterday, I'm really newbie on python and django.
Create a custom save() method sth like that :
class Test(models.Model):
test_id = models.IntegerField(primary_key=True)
amount = models.IntegerField()
name = models.CharField(max_length=50)
timestamp = models.DateTimeField()
def save(self, *args, **kwargs):
self.name = 'hello' if self.amount == 300 else 'world'
super(Test, self).save(*args, **kwargs)
Either you can handle this on model's custom save method or on serializer level (ModelSerializer) if you're using rest apis. The recommended way is to handle these kind of data manipulation in your model's serializer. Use a custom create/update method in your serializer (The logic on both solutions remains the same):
class TestSerializer(serializers.ModelSerializer):
amount = serilaizers.IntegerField()
name = serilaizers.CharField()
class Meta:
model = Test
fields = [
'test_id',
'amount',
'name',
'timestamp',
]
def create(self, validated_data):
if validated_data['amount'] > 1200:
validated_data['name'] = 'dry'
elif validated_data['amount'] == 0:
validated_data['name'] = 'wet'
elif 900 <= validated_data['amount'] <= 1200:
validated_data['name'] = 'light'
elif 500 <= validated_data['amount'] < 900:
validated_data['name'] = 'medium'
elif 0 < validated_data['amount'] < 500:
validated_data['name'] = 'heavy'
return Test.objects.create(**validated_data)

Django form initial pass a list to one attribute

I met an issue when I want to pass a list of value to one attribute during initial the form.
The whole process of what I am doing is:
1. User defines a number N.
2. I display N text field.
3. User fills in data and I store in the database.
4. User wants to modify the data -> which is the issue I have when. I want to initial the form with current data.
Here is my model.py
class PageComponent(models.Model):
componentName=models.CharField(max_length=50,null=True, verbose_name="Name")
type = models.ForeignKey(Type, on_delete=models.CASCADE)
user = models.ForeignKey(AS_User, on_delete=models.CASCADE, editable=False)
page = models.ForeignKey(CommunityPage, on_delete=models.CASCADE, editable=False)
STATUS = (
('a', 'Activated'),
('d', 'Deactivated'),
)
componentStatus=models.CharField(
max_length=1,
choices=STATUS,
blank=False,
default='d',
help_text='the current status of the page component', editable=False
)
textContent=models.TextField(max_length=10000, help_text="Enter a description for your component", null=True, blank=True)
photoContent=models.ImageField(upload_to=component_directory_path, null=True, blank=True, verbose_name="Photo")
videoContent=models.FileField(upload_to=component_directory_path, null=True, blank=True, verbose_name="Video")
def __str__(self):
return self.componentName
class PageComponent_SeasonalText(models.Model):
pageStextComponent = models.ForeignKey(PageComponent, on_delete=models.CASCADE)
seasonalText = models.CharField(max_length=10001)
Here is my form.py
class SeasonalTextForm(forms.Form):
componentName = forms.CharField(label=_('Enter title'),max_length=40)
def __init__(self, *args, **kwargs):
seasonTexts = kwargs.pop('extra')
super(SeasonalTextForm, self).__init__(*args, **kwargs)
# self.cleaned_data = {}
for i in range(0, seasonTexts):
field_name = 'seasonText_%s' % (i,)
self.fields[field_name] = forms.CharField(widget=forms.Textarea(attrs={'rows':10, 'cols':51}))
#set field label as placeholder for every field
for field in self.fields.values():
field.widget.attrs["placeholder"] = field.label
def clean(self):
seasonTexts = set()
i = 0
field_name = 'seasonText_%s' % (i,)
while self.cleaned_data.get(field_name):
seasonText = self.cleaned_data[field_name]
if seasonText in seasonTexts:
self.add_error(field_name, 'Duplicate')
else:
seasonTexts.add(seasonText)
i += 1
field_name='seasonText_%s' % (i,)
self.cleaned_data["seasonTexts"] = seasonTexts
def save(self):
for seasonalText in self.cleaned_data["seasonTexts"]:
PageComponent_SeasonalText.objects.create(pageStextComponent = PageComponent.pageStextComponent,seasonalText = seasonalText,)
Here is my view.py
def edit_stext(request, page_id, id):
page = get_object_or_404(CommunityPage, pk=page_id)
component = PageComponent.objects.get(id=id)
stext = PageComponent_SeasonalText.objects.filter(pageStextComponent=component)
numOfSeasons = page.numSeasons
if request.method == "POST":
stextEditor = SeasonalTextForm(request.POST, instance=stext, extra=numOfSeasons)
if stextEditor.is_valid():
stextEditor.save()
return HttpResponseRedirect(reverse('page', kwargs={'page_id' : page_id}))
else:
# stext1 = PageComponent_SeasonalText.objects.filter(pageStextComponent=component)
initial = {}
initial['componentName'] = component.componentName
for i in range(0,numOfSeasons):
st = stext[i].seasonalText
print(st)
initial['seasonalText_{}'.format(i)] = st
stextEditor = SeasonalTextForm(initial=initial, extra=numOfSeasons)
return render(request, 'editComponent_stext.html', {'stextEditor': stextEditor})
NB:
at the view.py, I have a print function to print the actual value of the attribute "seasonalText" and it is success. But it seems cannot be passed to the initial when I want to initial the form.
Thanks for all spending time to help me with this issue. Many thanks.
Screenshot for understanding:
print function gets the correct value
initialed form doesn't get the seasonalText value

Django : Updating model by overriding save method

This is my first model
from django.db import models
from django.contrib.auth.models import User
CHOICES = (('Earned Leave','Earned Leave'),('Casual Leave','Casual Leave'),('Sick Leave','Sick Leave'),('Paid Leave','Paid Leave'))
STATUS_CHOICES = (('0', 'Rejected'),('1', 'Accepted'),)
class Leave(models.Model):
employee_ID = models.CharField(max_length = 20)
name = models.CharField(max_length = 50)
user = models.ForeignKey(User, on_delete = models.CASCADE, null =True)
type_of_leave = models.CharField(max_length = 15, choices = CHOICES)
from_date = models.DateField()
to_date = models.DateField()
status = models.CharField(max_length = 15, choices = STATUS_CHOICES)
#property
def date_diff(self):
return (self.to_date - self.from_date).days
This is my second model
class History(models.Model):
first_name = models.CharField(max_length = 50)
last_name = models.CharField(max_length = 50)
employee_ID = models.CharField(max_length = 20)
earned_leave = models.IntegerField()
casual_leave = models.IntegerField()
sick_leave = models.IntegerField()
paid_leave =models.IntegerField()
Here upon saving the first model Leave, I have written to override save method like,
def save(self, *args, **kwargs):
super(Leave, self).save()
if self.employee_ID == History.employee_ID:
if self.status == '1':
if self.type_of_leave == 'Earned Leave':
history = History.objects.update(
earned_leave = self.date_diff,
)
But upon saving the first model, all the entries in the History model are getting updated. Where in the history table every user have a separate entry with user's details(first_name, last_name, employee_ID) and default values as 10 for the rest. Upon saving the Leave model only the entry that is associated with the employee_ID of Leave model should be updated in the History model. For that purpose i have given as if self.employee_ID == History.employee_ID: but it isn't working.
I've even tried as -
def save(self, *args, **kwargs):
super(Leave, self).save()
if self.employee_ID == History.employee_ID:
if self.status == '1':
if self.type_of_leave == 'Earned Leave':
history = History.objects.update(
earned_leave = History.earned_leave - self.date_diff,
)
But this is not working, nothing gets updated and get'a an error unsupported operand types
So, the base of the project is employee-leave-management. As the user applies for the leave and is accepted the number of days left should get updated in History table or model.
If there's any alternate method, share.
History.objects.update(...) does in fact update all the History objects. You should specify which ones you want to update in your query:
from django.db.models import F
def save(self, *args, **kwargs):
super(Leave, self).save()
if self.status == '1':
if self.type_of_leave == 'Earned Leave':
history = History.objects.filter(employee_id=self.employee_id).update(
earned_leave = F('earned_leave') - self.date_diff,
)
The F() expression here refers to the value of the column and will be computed by the database rather than in Python.

Django generate custom ID

I saw this answer but there is no specific answer yet. I want to create custom id that starts with letter. When a new record comes into database I want to change the id to A00001, .... A00002, .... A00010, ...A10000 etc. The id will be always in range 99999- 00001 so how can I do that?
my model is simple:
class Custom(models.Model):
id = models.AutoField(primary_key=True, editable=False)
The AutoField field is a kind of IntegerField field, so you can't use PKs as A00001 .
So, the possible way to achieve the requirement is to change the AutoField to CharField.
Technically you can use "String PK Field" But, you should be aware of the problems/performance issues if you are going to use that.
Here I found one nice SO post that explains the same - Strings as Primary Keys in SQL Database========================================================================
If you still really wish to migrate to String PKs, read the following
First you need to use the CharField instead of AutoField and override the save() method of model
from django.db.models import Max
class Custom(models.Model):
id = models.CharField(primary_key=True, editable=False, max_length=10)
name = models.CharField(max_length=100)
def save(self, **kwargs):
if not self.id:
max = Custom.objects.aggregate(id_max=Max('id'))['id_max']
self.id = "{}{:05d}".format('A', max if max is not None else 1)
super().save(*kwargs)
string as Primary Key not good idea if you plan to do references to the table, so i recommend you to add a property, for example:
class Custom(models.Model):
id = models.AutoField(primary_key=True, editable=False)
#property
def sid(self):
return "A%05d" % self.id
and to do queries you can do processing the input values, for example:
s_input = "A%05d" % 231 # 'A00231'
number = s_input[1:] # '00231'
input_id = int(number) # 231
I also have another way, That i use in my django project. Here are some code
def ids():
no = Employee.objects.count()
if no == None:
return 1
else:
return no + 1
emp_id = models.IntegerField(('Code'), default=ids, unique=True, editable=False)
id = models.CharField(primary_key=True, editable=False, max_length=30)
def save(self, **kwargs):
if not self.id:
self.id = "{}{:08d}".format('ABC', self.emp_id)
super().save(*kwargs)
It's better to create a new field for the custom id in the models and the process in the backend. You can set that as primary_key with unique=True and editable=False:
class Custom(models.Model):
id = models.Autofield(primary_key=True, editable=False, max_length=10)
uid= models.CharField(max_length=100, unique=True)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
self.set_uid() # calling the set_uid function
def set_uid(self):
if not self.uid: # if uid of the instance is blank
uid = "CUS" + str(self.id + (10 ** 5)) # generating the uid
customer= Custom.objects.get(id=self.id) # getting the instance
customer.uid = uid # allocating the value
customer.save() # saving the instance
def __str__(self):
return self.uid
Can also merge the set_uid() inside the save() where the function is called:
class Custom(models.Model):
id = models.Autofield(primary_key=True, editable=False, max_length=10)
uid= models.CharField(max_length=100, unique=True)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
if not self.uid: # if uid of the instance is blank
self.uid = "CUS" + str(self.id + (10 ** 5)) # generating the uid and allocating the value
self.save() # saving the instance
def __str__(self):
return self.uid
I tried to use answer of #JPG, but it has a bug.
The bug is becasue it can't auto increment.
I fixed the bug, and this my resultant code:
def save(self, **kwargs):
if not self.id:
max = YourModel.objects.aggregate(
id_max=models.Max('id'))['id_max']
if max is not None:
max += 1
else:
max = 100
self.id = "{:08d}".format(
max) # id from 100 to start
super().save(*kwargs)

Categories