From 9ca4821a1d1b50bce3b23334253a3a7c883fd2e3 Mon Sep 17 00:00:00 2001 From: Daniel Gilge <33256939+dgilge@users.noreply.github.com> Date: Fri, 23 Feb 2018 09:18:18 +0100 Subject: [PATCH 1/2] Add method and inner class example --- doc/topics/model.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/doc/topics/model.rst b/doc/topics/model.rst index 37963c5..f858dbf 100644 --- a/doc/topics/model.rst +++ b/doc/topics/model.rst @@ -49,6 +49,16 @@ And here is the equivalent class built using ``type()``: Any Django model that can be defined in the normal fashion can be made using ``type()``. +You can also define methods and inner classes this way: + +.. code-block:: python + + attrs = { + 'name': models.CharField(max_length=32), + '__unicode__': lambda self: '<{}> {}'.fromat(self.id, self.name), + 'Meta': type('Meta': (), {'ordering': '-id'}), + '__module__': 'myapp.models' + } Django's model cache -------------------- From 29463d7b0d2e86b6cacbb79cda7be3b7031aadea Mon Sep 17 00:00:00 2001 From: Daniel Gilge <33256939+dgilge@users.noreply.github.com> Date: Fri, 23 Feb 2018 09:27:04 +0100 Subject: [PATCH 2/2] Change example for Python 3 useage --- doc/topics/model.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/topics/model.rst b/doc/topics/model.rst index f858dbf..61811b9 100644 --- a/doc/topics/model.rst +++ b/doc/topics/model.rst @@ -55,7 +55,7 @@ You can also define methods and inner classes this way: attrs = { 'name': models.CharField(max_length=32), - '__unicode__': lambda self: '<{}> {}'.fromat(self.id, self.name), + '__str__': lambda self: '<{}> {}'.format(self.id, self.name), 'Meta': type('Meta': (), {'ordering': '-id'}), '__module__': 'myapp.models' }