5

I'm using south to do schema and data migration in Django. I have a model like this:

class ModelFoo(models.Model):
    first_name = models.CharField()
    last_name = models.CharField()

    @property
    def full_name(self):
        return self.first_name + self.last_name

And in south data migration code, I want to use full_name directly, like this:

foo.full_name

But I got an error:

AttributeError: 'ModelFoo' object has no attribute 'full_name'

What's going wrong here? I sure it's OK to use 'full_name' in the view code of Django, why it fails here?

Daniel Dai
  • 1,019
  • 11
  • 24
  • 1
    see http://stackoverflow.com/questions/3314173/how-to-call-a-static-methods-on-a-django-model-class-during-a-south-migration?rq=1 – Jayesh Bhoi Aug 05 '15 at 09:12
  • thx, It sounds like a good reason, but some times it's really a lot of pain for not allowed to use it. Maybe there should be some settings that can change be behavior, and the developer is responsible for keep method there in later versions of the code, if he/she turn it on. – Daniel Dai Aug 05 '15 at 09:23

1 Answers1

1

As @Jayesh's link explains, you should not do this, because the methods could disappear later from your code, and it would break your migration.

However, you could be pretty sure that you will run this migration once, and never come back, or if for any other reason you want to risk this and reuse your property, maybe because it is ugly heavy, then you would accomplish it by importing your real model in migration and using that property, like this:

from models import ModelFoo
ModelFoo.full_name.fget(foo)

where foo is the variable from your example (the instance of south migration model class).

That is how you call a property from another class, as explained here.

smido
  • 771
  • 6
  • 14