2007-09-14 18:36:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
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.
|
|
|
|
"""
|
|
|
|
|
2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2011-07-13 09:35:51 +00:00
|
|
|
from django.db import models
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2011-10-13 18:04:12 +00:00
|
|
|
from .fields import MyAutoField
|
|
|
|
|
2009-06-08 04:50:24 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
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
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
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'
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2009-06-08 04:50:24 +00:00
|
|
|
class Bar(models.Model):
|
|
|
|
id = MyAutoField(primary_key=True, db_index=True)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2009-06-08 04:50:24 +00:00
|
|
|
return repr(self.pk)
|
|
|
|
|
|
|
|
|
|
|
|
class Foo(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
bar = models.ForeignKey(Bar, models.CASCADE)
|