apple_pie
apple_pie

Reputation: 339

approaching django model fields through a field name mapping

Django newbie here,

I have several types of models, in each of them the fields have different names (e.g. first_name, forename, prenom) and I want each of the models to contain a mapping so that I can easily approach each of the fields using one conventional name (e.g. first_name for all of the field names). what's a good way of doing this?

Upvotes: 1

Views: 1162

Answers (3)

tux21b
tux21b

Reputation: 94739

I think the best way would be to use conventional names in your models and provide only one obvious way to access it. If you don't wan't to change the database columns too, you can use the db_column option. Example:

class Person(models.Model):
    first_name = models.CharField(max_length=255, db_column='prenom')

class Customer(models.Model):
    first_name = models.CharField(max_length=255, db_column='forename')

class Worker(models.Model):
    first_name = models.CharField(max_length=255) # column is also called "first_name"

If you need to provide different ways to access the members (which I would try to avoid!) you can still add properties to each model.

Upvotes: 4

jturnbull
jturnbull

Reputation: 720

You could define a @property on each of your models, like this:

class Personage(models.Model):
    prenom = models.CharField(max_length=255)

    @property
    def first_name(self):
        return self.prenom

then you can just reference your new property like this:

personage = Personage.objects.all()[0]
personage.first_name

Upvotes: 3

Oli
Oli

Reputation: 239878

You could make your models all inherit a new model that has a property function defined to get/set the right variable.

class BaseNameClass(models.Model)

    def getfname(self):
        if hasattr(self, 'first_name'): return self.first_name
        if hasattr(self, 'prenom'): return self.prenom
        if hasattr(self, 'forename'): return self.forename

    def setfname(self, x):
        if hasattr(self, 'first_name'): self.first_name = x
        if hasattr(self, 'prenom'): self.prenom = x
        if hasattr(self, 'forename'): self.forename = x

    firstname = property(getfname, setfname)

And then change your models to all inherit from that. It will be slightly slower but we're talking nano and milliseconds.

If you had an descendant object called person, you'd access the name simply by:

print person.firstname
person.firstname = "oli"
print person.firstname

Upvotes: 0

Related Questions