1

I have a populated model (There is data in the model I need to preserve/use)named 'FeatureTag' that I need to rename to simply 'Feature'.

class FeatureTag(models.Model):
        name = models.CharField(max_length=48, unique=True)
        data = models.ManyToManyField(Product)
        def __unicode__(self):
                return self.name

3 Questions:

Is this something that can be done with Django?

Is this something that can be done with South?

Is there a way to rename the return statement for the model to reflect a different name?

THANKS!!!!

pythondjango
  • 1,561
  • 2
  • 13
  • 23
  • 1
    some good answers in http://stackoverflow.com/questions/2862979/easiest-way-to-rename-a-model-using-django-south – gladysbixly Mar 25 '11 at 18:30
  • re 3: yes, rename class name, add Meta option `db_table = 'appname_featuretag'` – Jerzyk Mar 25 '11 at 18:41
  • Just add this to the renamed model?: class Meta: db_table = 'appname_featuretag' – pythondjango Mar 25 '11 at 18:48
  • yes, but this will modify the database table name mapped to this model - is this what you wanted to do? – Lin Mar 25 '11 at 18:52
  • Yea, but I also need the new model to pull the data from the old db table. I added the meta but the data isn't being pulled. – pythondjango Mar 25 '11 at 19:08
  • what is exact table name - replace "appname" with our app. @Li db table *will not* be modified – Jerzyk Mar 25 '11 at 19:39
  • GOT IT TO WORK. The db_table was giving me problems so I renamed the table then went into PSQL admin and renamed everything over. Thanks for the help. – pythondjango Mar 25 '11 at 20:08
  • @Jerzyk - just to clarify, using db_table will not modify the actual table in the database, but the django model will now be mapped to a database table with a different name (unless db_table == the previous name). – Lin Mar 26 '11 at 17:42
  • @li correct, but as you can see question was about changing name of the model not underlaying database table name :) – Jerzyk Mar 26 '11 at 19:52

1 Answers1

2

For your 3 questions:

  • Yes, you can rename your Django's model, just make sure to fix all the references to it.
  • Yes, you can do this with South. You might need to edit the files generated by south - so make sure to check the code that is generated.
  • What function do you want to override and why? What is your use case?
Lin
  • 2,445
  • 5
  • 27
  • 37
  • Well, I renamed the model and all references, but when I ran syncdb, it claimed the contents were stale and needed to be deleted. I need to preserve the data in the model while renaming. – pythondjango Mar 25 '11 at 18:37
  • You would need to edit automatically generated South migrations as it would recognise the rename as a delete and a new model. There is a rename_table in South however so it can be done with a small amount of effort. http://south.aeracode.org/docs/databaseapi.html#methods – Stephen Paulger Mar 25 '11 at 18:38
  • It's not the contents of the table that it's saying is stale it's the ContentTypes, which is a table that tracks what models there are, when you remove a model (or rename it) that data becomes out of date. It should be fine to agree to remove them. As always take a backup first if it's live data. – Stephen Paulger Mar 25 '11 at 18:39