PageRenderTime 207ms CodeModel.GetById 80ms app.highlight 52ms RepoModel.GetById 70ms app.codeStats 0ms

/tests/regressiontests/m2m_through_regress/models.py

https://code.google.com/p/mango-py/
Python | 77 lines | 51 code | 21 blank | 5 comment | 0 complexity | 7ce860e12412ea793cb0f9c0182337cc MD5 | raw file
 1from datetime import datetime
 2
 3from django.contrib.auth.models import User
 4from django.core import management
 5from django.db import models
 6
 7
 8# Forward declared intermediate model
 9class Membership(models.Model):
10    person = models.ForeignKey('Person')
11    group = models.ForeignKey('Group')
12    price = models.IntegerField(default=100)
13
14    def __unicode__(self):
15        return "%s is a member of %s" % (self.person.name, self.group.name)
16
17# using custom id column to test ticket #11107
18class UserMembership(models.Model):
19    id = models.AutoField(db_column='usermembership_id', primary_key=True)
20    user = models.ForeignKey(User)
21    group = models.ForeignKey('Group')
22    price = models.IntegerField(default=100)
23
24    def __unicode__(self):
25        return "%s is a user and member of %s" % (self.user.username, self.group.name)
26
27class Person(models.Model):
28    name = models.CharField(max_length=128)
29
30    def __unicode__(self):
31        return self.name
32
33class Group(models.Model):
34    name = models.CharField(max_length=128)
35    # Membership object defined as a class
36    members = models.ManyToManyField(Person, through=Membership)
37    user_members = models.ManyToManyField(User, through='UserMembership')
38
39    def __unicode__(self):
40        return self.name
41
42# A set of models that use an non-abstract inherited model as the 'through' model.
43class A(models.Model):
44    a_text = models.CharField(max_length=20)
45
46class ThroughBase(models.Model):
47    a = models.ForeignKey(A)
48    b = models.ForeignKey('B')
49
50class Through(ThroughBase):
51    extra = models.CharField(max_length=20)
52
53class B(models.Model):
54    b_text = models.CharField(max_length=20)
55    a_list = models.ManyToManyField(A, through=Through)
56
57
58# Using to_field on the through model
59class Car(models.Model):
60    make = models.CharField(max_length=20, unique=True)
61    drivers = models.ManyToManyField('Driver', through='CarDriver')
62
63    def __unicode__(self, ):
64        return self.make
65
66class Driver(models.Model):
67    name = models.CharField(max_length=20, unique=True)
68
69    def __unicode__(self, ):
70        return self.name
71
72class CarDriver(models.Model):
73    car = models.ForeignKey('Car', to_field='make')
74    driver = models.ForeignKey('Driver', to_field='name')
75
76    def __unicode__(self, ):
77        return u"pk=%s car=%s driver=%s" % (str(self.pk), self.car, self.driver)