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