I have my django model Customer
which consists of these fields;
'Customer_ID', 'Name', 'Gender', 'Age', 'Nationality', 'Address', 'Account_Type', 'Salary', 'Balance', 'Employer_Stability', 'Customer_Loyalty', 'Residential_Status' and 'Service_Level'
where Service_Level
= Silver
, Gold
or Platinum
.
I have managed to create a custom-admin-action to just update the Service_Level without any condition as shown below;
def allocate_service(ModelAdmin, request, queryset):
queryset.update(Service_Level=2)
@admin.register(models.Customer)
class CustomerAdmin(admin.ModelAdmin):
icon = '<i class="material-icons">account_box</i>'
list_display = ('Customer_ID', 'Name', 'Gender', 'Nationality',
'Account_Type', 'Salary', 'Balance', 'Service_Level')
list_per_page = 10
list_filter = ('Nationality', 'Gender')
actions = [allocate_service ]
I would like to add an action that assigns the Service_Level to a customer/ customers depending on the values of the bold features above (Age, Salary etc.).
e.g.
when Age > 25 and Salary >= 800 and Account_Type == Savings
then Service_Level = Platinum
.
my Models are as follows:
class Service(models.Model):
#service_no = models.CharField(primary_key=True, max_length=4)
service_name = models.CharField(primary_key=True, max_length=40)
service_description = models.TextField(default='')
class Meta:
db_table = 'services'
ordering = ['service_name']
def __str__(self):
return self.service_name
# Customer Model: too big so I ommited the other fields here
class Customer(models.Model):
Service_Level = models.ForeignKey(Service, on_delete=models.CASCADE,
db_column='service_name', null=True, blank=True)
I removed the option to use int key on Service_Level
I am not sure how I am supposed to go about it. Help will be appreciated