diff options
| author | Flavio Curella <flavio.curella@gmail.com> | 2015-07-22 09:43:21 -0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-07-27 18:28:13 -0400 |
| commit | c2e70f02653519db3a49cd48f5158ccad7434d25 (patch) | |
| tree | c0f421a6b0c26a7716c380b3e360fecc74d553fb /docs/topics | |
| parent | 87d55081ea398c65b2503d22ed3907a9175ec729 (diff) | |
Fixed #21127 -- Started deprecation toward requiring on_delete for ForeignKey/OneToOneField
Diffstat (limited to 'docs/topics')
| -rw-r--r-- | docs/topics/auth/customizing.txt | 7 | ||||
| -rw-r--r-- | docs/topics/class-based-views/generic-display.txt | 2 | ||||
| -rw-r--r-- | docs/topics/class-based-views/generic-editing.txt | 2 | ||||
| -rw-r--r-- | docs/topics/db/examples/many_to_one.txt | 2 | ||||
| -rw-r--r-- | docs/topics/db/examples/one_to_one.txt | 8 | ||||
| -rw-r--r-- | docs/topics/db/managers.txt | 2 | ||||
| -rw-r--r-- | docs/topics/db/models.txt | 32 | ||||
| -rw-r--r-- | docs/topics/db/queries.txt | 6 | ||||
| -rw-r--r-- | docs/topics/forms/modelforms.txt | 14 | ||||
| -rw-r--r-- | docs/topics/i18n/translation.txt | 16 | ||||
| -rw-r--r-- | docs/topics/serialization.txt | 4 |
11 files changed, 67 insertions, 28 deletions
diff --git a/docs/topics/auth/customizing.txt b/docs/topics/auth/customizing.txt index 7d3f6fee9e..289237be18 100644 --- a/docs/topics/auth/customizing.txt +++ b/docs/topics/auth/customizing.txt @@ -312,7 +312,7 @@ you might create an Employee model:: from django.contrib.auth.models import User class Employee(models.Model): - user = models.OneToOneField(User) + user = models.OneToOneField(User, on_delete=models.CASCADE) department = models.CharField(max_length=100) Assuming an existing Employee Fred Smith who has both a User and Employee @@ -443,7 +443,10 @@ different User model. from django.db import models class Article(models.Model): - author = models.ForeignKey(settings.AUTH_USER_MODEL) + author = models.ForeignKey + settings.AUTH_USER_MODEL, + on_delete=models.CASCADE, + ) When connecting to signals sent by the ``User`` model, you should specify the custom model using the :setting:`AUTH_USER_MODEL` setting. For example:: diff --git a/docs/topics/class-based-views/generic-display.txt b/docs/topics/class-based-views/generic-display.txt index f3d922ab3a..98b8955020 100644 --- a/docs/topics/class-based-views/generic-display.txt +++ b/docs/topics/class-based-views/generic-display.txt @@ -104,7 +104,7 @@ We'll be using these models:: class Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField('Author') - publisher = models.ForeignKey(Publisher) + publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE) publication_date = models.DateField() Now we need to define a view:: diff --git a/docs/topics/class-based-views/generic-editing.txt b/docs/topics/class-based-views/generic-editing.txt index be087d7758..0ada8db42a 100644 --- a/docs/topics/class-based-views/generic-editing.txt +++ b/docs/topics/class-based-views/generic-editing.txt @@ -204,7 +204,7 @@ the foreign key relation to the model: class Author(models.Model): name = models.CharField(max_length=200) - created_by = models.ForeignKey(User) + created_by = models.ForeignKey(User, on_delete=models.CASCADE) # ... diff --git a/docs/topics/db/examples/many_to_one.txt b/docs/topics/db/examples/many_to_one.txt index e43809fd1c..10020a83cb 100644 --- a/docs/topics/db/examples/many_to_one.txt +++ b/docs/topics/db/examples/many_to_one.txt @@ -17,7 +17,7 @@ To define a many-to-one relationship, use :class:`~django.db.models.ForeignKey`: class Article(models.Model): headline = models.CharField(max_length=100) pub_date = models.DateField() - reporter = models.ForeignKey(Reporter) + reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE) def __str__(self): # __unicode__ on Python 2 return self.headline diff --git a/docs/topics/db/examples/one_to_one.txt b/docs/topics/db/examples/one_to_one.txt index f2a3f54508..311ff75d8d 100644 --- a/docs/topics/db/examples/one_to_one.txt +++ b/docs/topics/db/examples/one_to_one.txt @@ -16,7 +16,11 @@ In this example, a ``Place`` optionally can be a ``Restaurant``:: return "%s the place" % self.name class Restaurant(models.Model): - place = models.OneToOneField(Place, primary_key=True) + place = models.OneToOneField( + Place, + on_delete=models.CASCADE, + primary_key=True, + ) serves_hot_dogs = models.BooleanField(default=False) serves_pizza = models.BooleanField(default=False) @@ -24,7 +28,7 @@ In this example, a ``Place`` optionally can be a ``Restaurant``:: return "%s the restaurant" % self.place.name class Waiter(models.Model): - restaurant = models.ForeignKey(Restaurant) + restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE) name = models.CharField(max_length=50) def __str__(self): # __unicode__ on Python 2 diff --git a/docs/topics/db/managers.txt b/docs/topics/db/managers.txt index 64d484e85d..bd46a34c07 100644 --- a/docs/topics/db/managers.txt +++ b/docs/topics/db/managers.txt @@ -87,7 +87,7 @@ returns a list of all ``OpinionPoll`` objects, each with an extra objects = PollManager() class Response(models.Model): - poll = models.ForeignKey(OpinionPoll) + poll = models.ForeignKey(OpinionPoll, on_delete=models.CASCADE) person_name = models.CharField(max_length=50) response = models.TextField() diff --git a/docs/topics/db/models.txt b/docs/topics/db/models.txt index c21f231808..ed13e46374 100644 --- a/docs/topics/db/models.txt +++ b/docs/topics/db/models.txt @@ -99,7 +99,7 @@ Example:: instrument = models.CharField(max_length=100) class Album(models.Model): - artist = models.ForeignKey(Musician) + artist = models.ForeignKey(Musician, on_delete=models.CASCADE) name = models.CharField(max_length=100) release_date = models.DateField() num_stars = models.IntegerField() @@ -281,9 +281,17 @@ In this example, the verbose name is ``"first name"``:: :class:`~django.db.models.OneToOneField` require the first argument to be a model class, so use the :attr:`~Field.verbose_name` keyword argument:: - poll = models.ForeignKey(Poll, verbose_name="the related poll") + poll = models.ForeignKey( + Poll, + on_delete=models.CASCADE, + verbose_name="the related poll", + ) sites = models.ManyToManyField(Site, verbose_name="list of sites") - place = models.OneToOneField(Place, verbose_name="related place") + place = models.OneToOneField( + Place, + on_delete=models.CASCADE, + verbose_name="related place", + ) The convention is not to capitalize the first letter of the :attr:`~Field.verbose_name`. Django will automatically capitalize the first @@ -317,7 +325,7 @@ For example, if a ``Car`` model has a ``Manufacturer`` -- that is, a pass class Car(models.Model): - manufacturer = models.ForeignKey(Manufacturer) + manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE) # ... You can also create :ref:`recursive relationships <recursive-relationships>` (an @@ -331,7 +339,10 @@ above) be the name of the model, lowercase. You can, of course, call the field whatever you want. For example:: class Car(models.Model): - company_that_makes_it = models.ForeignKey(Manufacturer) + company_that_makes_it = models.ForeignKey( + Manufacturer, + on_delete=models.CASCADE, + ) # ... .. seealso:: @@ -445,8 +456,8 @@ something like this:: return self.name class Membership(models.Model): - person = models.ForeignKey(Person) - group = models.ForeignKey(Group) + person = models.ForeignKey(Person, on_delete=models.CASCADE) + group = models.ForeignKey(Group, on_delete=models.CASCADE) date_joined = models.DateField() invite_reason = models.CharField(max_length=64) @@ -621,7 +632,12 @@ just refer to the other model class wherever needed. For example:: class Restaurant(models.Model): # ... - zip_code = models.ForeignKey(ZipCode) + zip_code = models.ForeignKey( + ZipCode, + on_delete=models.SET_NULL, + blank=True, + null=True, + ) Field name restrictions ----------------------- diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt index c7ff96c8e3..d7617e8b48 100644 --- a/docs/topics/db/queries.txt +++ b/docs/topics/db/queries.txt @@ -1160,8 +1160,8 @@ Example:: You can override the ``FOO_set`` name by setting the :attr:`~django.db.models.ForeignKey.related_name` parameter in the :class:`~django.db.models.ForeignKey` definition. For example, if the ``Entry`` -model was altered to ``blog = ForeignKey(Blog, related_name='entries')``, the -above example code would look like this:: +model was altered to ``blog = ForeignKey(Blog, on_delete=models.CASCADE, +related_name='entries')``, the above example code would look like this:: >>> b = Blog.objects.get(id=1) >>> b.entries.all() # Returns all Entry objects related to Blog. @@ -1284,7 +1284,7 @@ model. For example:: class EntryDetail(models.Model): - entry = models.OneToOneField(Entry) + entry = models.OneToOneField(Entry, on_delete=models.CASCADE) details = models.TextField() ed = EntryDetail.objects.get(id=2) diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt index 2e75695f53..e9898c6fff 100644 --- a/docs/topics/forms/modelforms.txt +++ b/docs/topics/forms/modelforms.txt @@ -1119,7 +1119,7 @@ you have these two models:: name = models.CharField(max_length=100) class Book(models.Model): - author = models.ForeignKey(Author) + author = models.ForeignKey(Author, on_delete=models.CASCADE) title = models.CharField(max_length=100) If you want to create a formset that allows you to edit books belonging to @@ -1178,8 +1178,16 @@ need to resolve the ambiguity manually using ``fk_name``. For example, consider the following model:: class Friendship(models.Model): - from_friend = models.ForeignKey(Friend, related_name='from_friends') - to_friend = models.ForeignKey(Friend, related_name='friends') + from_friend = models.ForeignKey( + Friend, + on_delete=models.CASCADE, + related_name='from_friends', + ) + to_friend = models.ForeignKey( + Friend, + on_delete=models.CASCADE, + related_name='friends', + ) length_in_months = models.IntegerField() To resolve this, you can use ``fk_name`` to diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt index 79b466ee39..d976ddf2ef 100644 --- a/docs/topics/i18n/translation.txt +++ b/docs/topics/i18n/translation.txt @@ -355,8 +355,12 @@ You can mark names of :class:`~django.db.models.ForeignKey`, their :attr:`~django.db.models.Options.verbose_name` options:: class MyThing(models.Model): - kind = models.ForeignKey(ThingKind, related_name='kinds', - verbose_name=_('kind')) + kind = models.ForeignKey( + ThingKind, + on_delete=models.CASCADE, + related_name='kinds', + verbose_name=_('kind'), + ) Just like you would do in :attr:`~django.db.models.Options.verbose_name` you should provide a lowercase verbose name text for the relation as Django will @@ -391,8 +395,12 @@ with the ``short_description`` attribute:: from django.utils.translation import ugettext_lazy as _ class MyThing(models.Model): - kind = models.ForeignKey(ThingKind, related_name='kinds', - verbose_name=_('kind')) + kind = models.ForeignKey( + ThingKind, + on_delete=models.CASCADE, + related_name='kinds', + verbose_name=_('kind'), + ) def is_mouse(self): return self.kind.type == MOUSE_TYPE diff --git a/docs/topics/serialization.txt b/docs/topics/serialization.txt index b48c625fe8..e34553699d 100644 --- a/docs/topics/serialization.txt +++ b/docs/topics/serialization.txt @@ -344,7 +344,7 @@ Consider the following two models:: class Book(models.Model): name = models.CharField(max_length=100) - author = models.ForeignKey(Person) + author = models.ForeignKey(Person, on_delete=models.CASCADE) Ordinarily, serialized data for ``Book`` would use an integer to refer to the author. For example, in JSON, a Book might be serialized as:: @@ -514,7 +514,7 @@ example above:: class Book(models.Model): name = models.CharField(max_length=100) - author = models.ForeignKey(Person) + author = models.ForeignKey(Person, on_delete=models.CASCADE) def natural_key(self): return (self.name,) + self.author.natural_key() |
