PowerLove
PowerLove

Reputation: 303

Changing the field name from inherited model?

Is changing names of fields even possible? so I have two models,

class ChangeLog(IpHandlerModel):
    id = models.AutoField(primary_key=True)
    change_operations = models.CharField(max_length=1, choices=CHANGE_OPERATION_CHOICES)
    change_type = models.CharField(max_length=3, choices=CHANGE_TYPE_CHOICES)
    cust_uuid = models.UUIDField(default=uuid.uuid1)
    ip_address = models.GenericIPAddressField()
    ip_assign_ts = models.DateTimeField()
    ip_source = models.CharField(max_length=4, choices=IP_ASSIGNMENT_SOURCE_CHOICES)
    ip_source_device = models.CharField(max_length=255, null=True, blank=True)
    ip_unassign_ts = models.DateTimeField(null=True, blank=True)
    is_hacker_alert_cust = models.BooleanField()
    mac_address = models.CharField(max_length=12)
    mac_assign_ts = models.DateTimeField()
    mac_unassign_ts = models.DateTimeField(null=True, blank=True)
    status = models.CharField(max_length=7, choices=STATUS_CHOICES, default='SEND')
    error_count = models.IntegerField(default=0)

class ChangeLogArchive(ChangeLog):
    def __init__(self, *args, **kwargs):
    super(ChangeLogArchive, self).__init__(*args, **kwargs)

So, ChangeLogArchive inherits ChangeLog, and I want some of the field names in the ChangeLog to be changed. For example, ip_assign_ts to original_ip_assign_ts

Would this be even possible?

Upvotes: 1

Views: 442

Answers (2)

Benyamin Jafari
Benyamin Jafari

Reputation: 33946

Yes, it is. In fact, you can simply override the field (e.g. override it to None) in the inherited class and create a new field instead:

class ChangeLog(IpHandlerModel):
    ...
    ip_assign_ts = models.DateTimeField()
    ...


class ChangeLogArchive(ChangeLog):
    ip_assign_ts = None
    original_ip_assign_ts = models.DateTimeField()

Then you can check the respective migration file to ensure that.

Upvotes: 0

Lars Olafsson
Lars Olafsson

Reputation: 308

I am not sure if it is possible to change it. But what you could do, is to create a new field with the new name. And link it to the other field. So any save of either ChangeLog or ChangeLogArchive overwrites the value in the new field original_ip_assign_ts.

Just an idea.

Upvotes: 1

Related Questions