2006-05-02 01:31:56 +00:00
|
|
|
"""
|
2014-09-24 05:13:13 +00:00
|
|
|
Using a custom primary key
|
2006-05-02 01:31:56 +00: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 09:35:51 +00:00
|
|
|
from django.db import models
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2021-02-13 08:58:24 +00:00
|
|
|
from .fields import MyAutoField, MyWrapperField
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2009-06-08 04:50:24 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
class Employee(models.Model):
|
2013-11-03 04:36:09 +00:00
|
|
|
employee_code = models.IntegerField(primary_key=True, db_column="code")
|
2007-08-05 05:14:46 +00:00
|
|
|
first_name = models.CharField(max_length=20)
|
|
|
|
last_name = models.CharField(max_length=20)
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
class Meta:
|
|
|
|
ordering = ("last_name", "first_name")
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2012-06-07 16:08:47 +00:00
|
|
|
return "%s %s" % (self.first_name, self.last_name)
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
class Business(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=20, primary_key=True)
|
2006-05-02 01:31:56 +00:00
|
|
|
employees = models.ManyToManyField(Employee)
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name_plural = "businesses"
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2009-06-08 04:50:24 +00:00
|
|
|
class Bar(models.Model):
|
2021-02-13 08:58:24 +00:00
|
|
|
id = MyWrapperField(primary_key=True, db_index=True)
|
2009-06-08 04:50:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Foo(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
bar = models.ForeignKey(Bar, models.CASCADE)
|
2021-02-13 08:58:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CustomAutoFieldModel(models.Model):
|
|
|
|
id = MyAutoField(primary_key=True)
|