Code

Ticket #2536: models.py

File models.py, 4.5 KB (added by ramiro <rm0 _at_ gmx.net>, 7 years ago)
Line 
1from django.db import models
2
3# Create your models here.
4 
5# ---------------------------------------------------------------------
6# This reproduces the original report. The admin app show
7# a traceback when you click in the 'Add Child' button.
8class Parent(models.Model):
9    name = models.CharField(maxlength=100)
10    bestchild = models.ForeignKey("Child", null=True, related_name="favoured_by")
11    class Meta:
12        verbose_name_plural = "01. Parents"
13
14    class Admin:
15        pass
16
17class Child(models.Model):
18    """
19    manipulator = Child.AddManipulator()
20    """
21    name = models.CharField(maxlength=100)
22    parent = models.ForeignKey(Parent)
23    class Meta:
24        verbose_name_plural = "02. Childs"
25
26    class Admin:
27        pass
28
29# ---------------------------------------------------------------------
30# This seems to demonstrate that the problem isn't related to the
31# forward FK declaration from Poll to Choice that uses the "Choice"
32# string notation as the related model spcification.
33# The admin traceback still exists but this time when you click in the
34# 'Add Choice' button.
35class Poll(models.Model):
36    question = models.CharField(maxlength=200)
37    choice = models.ForeignKey("Choice")
38    class Meta:
39        verbose_name_plural = "03. Polls"
40
41    class Admin:
42        pass
43
44class Choice(models.Model):
45    choice = models.CharField(maxlength=200)
46    assigned_poll = models.ForeignKey(Poll, null=True, related_name="preset_choice")
47    class Meta:
48        verbose_name_plural = "04. Choices"
49
50    class Admin:
51        pass
52
53# ---------------------------------------------------------------------
54# This seems to demonstrate the problems isn't related to the null=True
55# option. When it is used in both FK the 'Add book' button click still
56# generates a traceback.
57class Author(models.Model):
58    name = models.CharField(maxlength=50)
59    writting = models.ForeignKey("Book", null=True, related_name="probable_author")
60    class Meta:
61        verbose_name_plural = "05. Authors"
62
63    class Admin:
64        pass
65
66class Book(models.Model):
67    title = models.CharField(maxlength=255)
68    author = models.ForeignKey(Author, null=True, related_name="preferred_work")
69    class Meta:
70        verbose_name_plural = "06. Books"
71
72    class Admin:
73        pass
74
75# ---------------------------------------------------------------------
76# FK attribute names changed to differ from their respective related
77# model names, 'related_name' options dropped.
78# No traceback when clicking the 'Add reported' nor the 'Add article'
79# buttons.
80class Reporter(models.Model):
81    name = models.CharField(maxlength=50)
82    art = models.ForeignKey("Article", null=True)
83    class Meta:
84        verbose_name_plural = "07. Reporters"
85
86    class Admin:
87        pass
88
89class Article(models.Model):
90    headline = models.CharField(maxlength=255)
91    auth = models.ForeignKey(Reporter)
92    class Meta:
93        verbose_name_plural = "08. Articles"
94
95    class Admin:
96        pass
97
98# ---------------------------------------------------------------------
99# This seems to indicate the problem is related to the fact the
100# FK name is equal to the related model name but in lowercase
101# (something already hinted by bmurdock@gmail.com).
102# Traceback when BOTH 'Add painter' and 'Add Painting' buttons are cli-
103# cked.
104class Painter(models.Model):
105    name = models.CharField(maxlength=50)
106    painting = models.ForeignKey("Painting", null=True, related_name="probable_author")
107    class Meta:
108        verbose_name_plural = "09. Painters"
109
110    class Admin:
111        pass
112
113class Painting(models.Model):
114    name = models.CharField(maxlength=255)
115    painter = models.ForeignKey(Painter, related_name="best_priced_painting")
116    class Meta:
117        verbose_name_plural = "10. Paintings"
118
119    class Admin:
120        pass
121
122# ---------------------------------------------------------------------
123# Just to be sure it has nothing to do with something reported in #999
124# No related_name options, use the related model name in plural as the
125# FK attribute name.
126# No tracebacks.
127class Owner(models.Model):
128    name = models.CharField(maxlength=50)
129    pets = models.ForeignKey("Pet", null=True)
130    class Meta:
131        verbose_name_plural = "11. Owners"
132
133    class Admin:
134        pass
135
136class Pet(models.Model):
137    nick = models.CharField(maxlength=255)
138    owners = models.ForeignKey(Owner)
139    class Meta:
140        verbose_name_plural = "12. Pets"
141
142    class Admin:
143        pass