This can be done in the admin, but there is no very easy way to it. In addition, I would like to advise you to keep most of the business logic in your models, so you will not be dependent on the Django administrator.
It might be easier (and perhaps even better) if you have two separate fields on your model. Then add a method of your model that combines them.
For example:
class MyModel(models.model): field1 = models.CharField(max_length=10) field2 = models.CharField(max_length=10) def combined_fields(self): return '{} {}'.format(self.field1, self.field2)
Then in admin you can add combined_fields() as a read-only field:
class MyModelAdmin(models.ModelAdmin): list_display = ('field1', 'field2', 'combined_fields') readonly_fields = ('combined_fields',) def combined_fields(self, obj): return obj.combined_fields()
If you want to save combined_fields in the database, you can also save it when saving the model:
def save(self, *args, **kwargs): self.field3 = self.combined_fields() super(MyModel, self).save(*args, **kwargs)
gitaarik Jul 30 '13 at 13:35 2013-07-30 13:35
source share