models.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """
  2. Models can have a ``managed`` attribute, which specifies whether the SQL code
  3. is generated for the table on various manage.py operations.
  4. """
  5. from django.db import models
  6. # All of these models are created in the database by Django.
  7. class A01(models.Model):
  8. f_a = models.CharField(max_length=10, db_index=True)
  9. f_b = models.IntegerField()
  10. class Meta:
  11. db_table = 'a01'
  12. def __unicode__(self):
  13. return self.f_a
  14. class B01(models.Model):
  15. fk_a = models.ForeignKey(A01)
  16. f_a = models.CharField(max_length=10, db_index=True)
  17. f_b = models.IntegerField()
  18. class Meta:
  19. db_table = 'b01'
  20. # 'managed' is True by default. This tests we can set it explicitly.
  21. managed = True
  22. def __unicode__(self):
  23. return self.f_a
  24. class C01(models.Model):
  25. mm_a = models.ManyToManyField(A01, db_table='d01')
  26. f_a = models.CharField(max_length=10, db_index=True)
  27. f_b = models.IntegerField()
  28. class Meta:
  29. db_table = 'c01'
  30. def __unicode__(self):
  31. return self.f_a
  32. # All of these models use the same tables as the previous set (they are shadows
  33. # of possibly a subset of the columns). There should be no creation errors,
  34. # since we have told Django they aren't managed by Django.
  35. class A02(models.Model):
  36. f_a = models.CharField(max_length=10, db_index=True)
  37. class Meta:
  38. db_table = 'a01'
  39. managed = False
  40. def __unicode__(self):
  41. return self.f_a
  42. class B02(models.Model):
  43. class Meta:
  44. db_table = 'b01'
  45. managed = False
  46. fk_a = models.ForeignKey(A02)
  47. f_a = models.CharField(max_length=10, db_index=True)
  48. f_b = models.IntegerField()
  49. def __unicode__(self):
  50. return self.f_a
  51. # To re-use the many-to-many intermediate table, we need to manually set up
  52. # things up.
  53. class C02(models.Model):
  54. mm_a = models.ManyToManyField(A02, through="Intermediate")
  55. f_a = models.CharField(max_length=10, db_index=True)
  56. f_b = models.IntegerField()
  57. class Meta:
  58. db_table = 'c01'
  59. managed = False
  60. def __unicode__(self):
  61. return self.f_a
  62. class Intermediate(models.Model):
  63. a02 = models.ForeignKey(A02, db_column="a01_id")
  64. c02 = models.ForeignKey(C02, db_column="c01_id")
  65. class Meta:
  66. db_table = 'd01'
  67. managed = False
  68. #
  69. # These next models test the creation (or not) of many to many join tables
  70. # between managed and unmanaged models. A join table between two unmanaged
  71. # models shouldn't be automatically created (see #10647).
  72. #
  73. # Firstly, we need some models that will create the tables, purely so that the
  74. # tables are created. This is a test setup, not a requirement for unmanaged
  75. # models.
  76. class Proxy1(models.Model):
  77. class Meta:
  78. db_table = "unmanaged_models_proxy1"
  79. class Proxy2(models.Model):
  80. class Meta:
  81. db_table = "unmanaged_models_proxy2"
  82. class Unmanaged1(models.Model):
  83. class Meta:
  84. managed = False
  85. db_table = "unmanaged_models_proxy1"
  86. # Unmanged with an m2m to unmanaged: the intermediary table won't be created.
  87. class Unmanaged2(models.Model):
  88. mm = models.ManyToManyField(Unmanaged1)
  89. class Meta:
  90. managed = False
  91. db_table = "unmanaged_models_proxy2"
  92. # Here's an unmanaged model with an m2m to a managed one; the intermediary
  93. # table *will* be created (unless given a custom `through` as for C02 above).
  94. class Managed1(models.Model):
  95. mm = models.ManyToManyField(Unmanaged1)