PageRenderTime 132ms CodeModel.GetById 80ms app.highlight 4ms RepoModel.GetById 46ms app.codeStats 0ms

/tests/modeltests/custom_pk/models.py

https://code.google.com/p/mango-py/
Python | 42 lines | 38 code | 0 blank | 4 comment | 0 complexity | ab8795bdce0daa8d52373f50f9f5870c MD5 | raw file
 1# -*- coding: utf-8 -*-
 2"""
 314. Using a custom primary key
 4
 5By default, Django adds an ``"id"`` field to each model. But you can override
 6this behavior by explicitly adding ``primary_key=True`` to a field.
 7"""
 8
 9from django.conf import settings
10from django.db import models, transaction, IntegrityError, DEFAULT_DB_ALIAS
11
12from fields import MyAutoField
13
14class Employee(models.Model):
15    employee_code = models.IntegerField(primary_key=True, db_column = 'code')
16    first_name = models.CharField(max_length=20)
17    last_name = models.CharField(max_length=20)
18    class Meta:
19        ordering = ('last_name', 'first_name')
20
21    def __unicode__(self):
22        return u"%s %s" % (self.first_name, self.last_name)
23
24class Business(models.Model):
25    name = models.CharField(max_length=20, primary_key=True)
26    employees = models.ManyToManyField(Employee)
27    class Meta:
28        verbose_name_plural = 'businesses'
29
30    def __unicode__(self):
31        return self.name
32
33class Bar(models.Model):
34    id = MyAutoField(primary_key=True, db_index=True)
35
36    def __unicode__(self):
37        return repr(self.pk)
38
39
40class Foo(models.Model):
41    bar = models.ForeignKey(Bar)
42