Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Everyday Refactoring of a Django Project

D695739e92e74c0f35e5e2e4f270a1d7?s=47 Ana Yankova
September 04, 2015

Everyday Refactoring of a Django Project

D695739e92e74c0f35e5e2e4f270a1d7?s=128

Ana Yankova

September 04, 2015
Tweet

More Decks by Ana Yankova

Other Decks in Programming

Transcript

  1. EVERYDAY REFACTORING OF A DJANGO PROJECT

  2. We all want to build new features

  3. None
  4. The fast pace is fun but…

  5. Haste is waste

  6. What about technical debt?

  7. If it ain't broke, don't fix it. Rewrite the whole

    codebase!
  8. Test it!

  9. The later the change the higher the costs

  10. Everyday refactoring

  11. Usable TRUE Transparent Reasonable Exemplary

  12. Exemplary

  13. !

  14. class BarcodeScan(models.Model): barcode = models.CharField(unique=True, max_length=100) user = models.ForeignKey(‘accounts.UserProfile') created

    = models.DateTimeField(auto_now_add=True)
  15. class Food(models.Model): name = models.CharField(max_length=100) calories = models.Integer(default=0) class FoodBarcode(models.Model):

    barcode = models.CharField(unique=True, max_length=100) food = models.ForeignKey(Food) foodipedia/models.py
  16. " Foodipedia app ! Food Barcode model # Food model

    # ! Barcode Scan model " Barcodes app !
  17. None
  18. 1 add the new model to Foodipedia

  19. 1 add the new model to Foodipedia 2 Create the

    new app but leave FoodBarcode in Foodipedia
  20. 1 add the new model to Foodipedia 2 3 Create

    the new app but leave FoodBarcode in Foodipedia Create the new app and migrate FoodBarcode and its data
  21. 1 add the new model to Foodipedia 2 3 4

    Create the new app but leave FoodBarcode in Foodipedia Create the new app and refactor but use db_table to avoid schema changes Create the new app and migrate FoodBarcode and its data
  22. class FoodBarcode(models.Model): barcode = models.CharField(unique=True, max_length=100) food = models.ForeignKey('foodipedia.Food') class

    Meta: db_table = u'foodipedia_foodbarcode' class BarcodeScan(models.Model): barcode = models.CharField(unique=True, max_length=100) user = models.ForeignKey(‘accounts.UserProfile') created = models.DateTimeField(auto_now_add=True) barcodes/models.py
  23. class FoodBarcode(models.Model): barcode = models.CharField(unique=True, max_length=100) food = models.ForeignKey('foodipedia.Food') class

    Meta: db_table = u'foodipedia_foodbarcode' class BarcodeScan(models.Model): barcode = models.CharField(unique=True, max_length=100) user = models.ForeignKey(‘accounts.UserProfile') created = models.DateTimeField(auto_now_add=True) barcodes/models.py
  24. class Migration(DataMigration): def forwards(self, orm): orm['contenttypes.contenttype'].objects.filter( app_label='foodipedia', model='foodbarcode', ).update(app_label='barcodes') barcodes/migrations/0001_initial.py

    ./manage.py datamigration barcodes 0001_initial --frozen contenttypes
  25. class Migration(SchemaMigration): depends_on = (('barcodes', '0001_initial'),) def forwards(self, orm): pass

    foodipedia/migrations/0012_move_foodbarcode.py ./manage.py schemamigration foodipedia --auto
  26. Geek & Poke Font Awesome Practical Object-Oriented Design in Ruby

    by Sandi Metz hilite.me RESOURCES licensed under $ licensed under SIL OFL Stack Overflow: Easiest way to rename a model using Django/South? Two Scoops of Django: Best Practices for Django 1.6 by Daniel Greenfeld & Audrey Roy % % % % % % % %
  27. ana@lifesum.com anah @anhristova & ' (