I am working with a pre-existing database called Employee. I have three separate fields i'd like to combine into a single field, but I can't add an additional field to the pre-exisiting database.
I know the proper way to combine multiple fields into one field using python is
'%s - %s %s' % (self.username, self.firstname, self.lastname)
However, I can't call self outside the model, or at least i'm not sure where I would call self.
My end goal is to have a select box with the combined field a user can search either first, last, or account name. My current model looks like the following:
class Employee(models.Model):
staff_id = models.IntegerField(db_column = 'Employee_ID')
status_id = models.IntegerField(db_column = 'StatusID')
username = models.CharField(db_column = 'SamAccountName',primary_key = True, max_length = 31)
lastname = models.CharField(db_column = 'Surname', max_length = 63)
firstname = models.CharField(db_column = 'GivenName', max_length = 63)
title = models.CharField(db_column = 'Title', max_length = 127)
class Meta:
managed = False
db_table = '[Employee]'
I tried to add to my model, but when I call full_username it says the field doesn't exists, which is true because there isn't a field in the database. We aren't allowed to add a new field to the database.
def get_full_name(self):
full_username = '%s - %s %s' % (self.username, self.firstname, self.lastname)
return full_username.split()
Ideally i'd want my view to look something like this (i know it wont' work as is, i'd replace that with 'full_username):
activeuserlist = Employee.objects.filter(staff_id = '1').values_list('%s - %s %s' % (Employee.username, Employee.firstname, Employee.lastname), flat = True)
How would I get the full name added to my view, what am I missing with my logic or where would be the correct place to put it?