PageRenderTime 209ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/modeltests/custom_columns/models.py

https://code.google.com/p/mango-py/
Python | 40 lines | 36 code | 0 blank | 4 comment | 0 complexity | a68eb03134eee29b13a5d38cdd260e9f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. 17. Custom column/table names
  3. If your database column name is different than your model attribute, use the
  4. ``db_column`` parameter. Note that you'll use the field's name, not its column
  5. name, in API usage.
  6. If your database table name is different than your model name, use the
  7. ``db_table`` Meta attribute. This has no effect on the API used to
  8. query the database.
  9. If you need to use a table name for a many-to-many relationship that differs
  10. from the default generated name, use the ``db_table`` parameter on the
  11. ``ManyToManyField``. This has no effect on the API for querying the database.
  12. """
  13. from django.db import models
  14. class Author(models.Model):
  15. first_name = models.CharField(max_length=30, db_column='firstname')
  16. last_name = models.CharField(max_length=30, db_column='last')
  17. def __unicode__(self):
  18. return u'%s %s' % (self.first_name, self.last_name)
  19. class Meta:
  20. db_table = 'my_author_table'
  21. ordering = ('last_name','first_name')
  22. class Article(models.Model):
  23. headline = models.CharField(max_length=100)
  24. authors = models.ManyToManyField(Author, db_table='my_m2m_table')
  25. def __unicode__(self):
  26. return self.headline
  27. class Meta:
  28. ordering = ('headline',)