2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2014-09-24 13:13:13 +08:00
|
|
|
Using a custom primary key
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
By default, Django adds an ``"id"`` field to each model. But you can override
|
|
|
|
this behavior by explicitly adding ``primary_key=True`` to a field.
|
|
|
|
"""
|
|
|
|
|
2011-07-13 17:35:51 +08:00
|
|
|
from django.db import models
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-10-14 02:04:12 +08:00
|
|
|
from .fields import MyAutoField
|
|
|
|
|
2009-06-08 12:50:24 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Employee(models.Model):
|
2013-11-03 12:36:09 +08:00
|
|
|
employee_code = models.IntegerField(primary_key=True, db_column='code')
|
2007-08-05 13:14:46 +08:00
|
|
|
first_name = models.CharField(max_length=20)
|
|
|
|
last_name = models.CharField(max_length=20)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ('last_name', 'first_name')
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s %s" % (self.first_name, self.last_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Business(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=20, primary_key=True)
|
2006-05-02 09:31:56 +08:00
|
|
|
employees = models.ManyToManyField(Employee)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
|
|
|
verbose_name_plural = 'businesses'
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2009-06-08 12:50:24 +08:00
|
|
|
class Bar(models.Model):
|
|
|
|
id = MyAutoField(primary_key=True, db_index=True)
|
|
|
|
|
|
|
|
|
|
|
|
class Foo(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
bar = models.ForeignKey(Bar, models.CASCADE)
|