@@ -3,7 +3,7 @@ category: page
33slug: django-db-models-model-examples
44sortorder: 50002
55toc: False
6- sidebartitle: django.db.models.Model Examples
6+ sidebartitle: django.db.models.Model
77meta: Python code examples for the Model class within the django.db.models module of the Django project.
88
99
@@ -14,6 +14,34 @@ class is the superclass for all data stored in [Django](/django.html)
1414applications.
1515
1616
17+ ## Example 1 from django-audit-log
18+ [ django-audit-log] ( https://github.com/vvangelovski/django-audit-log ) is a
19+ [ code library] ( https://pypi.org/project/django-audit-log/ ) that tracks
20+ changes to [ Django] ( /django.html ) models. The source code is available
21+ under the
22+ [ BSD 3 "New" license] ( https://github.com/vvangelovski/django-audit-log/blob/master/LICENSE.txt ) .
23+
24+ [ ** django-audit-log/audit-log/models/__ init__ .py** ] ( https://github.com/vvangelovski/django-audit-log/blob/master/audit_log/models/__init__.py )
25+
26+ ``` python
27+ ~~ from django.db.models import Model
28+ from django.utils.translation import ugettext_lazy as _
29+ from audit_log.models.fields import CreatingUserField, CreatingSessionKeyField, LastUserField, LastSessionKeyField
30+
31+ ~~ class AuthStampedModel (Model ):
32+ """
33+ An abstract base class model that provides auth and session information
34+ fields.
35+ """
36+ created_by = CreatingUserField(verbose_name = _(" created by" ), related_name = " created_%(app_label)s _%(class)s _set" )
37+ created_with_session_key = CreatingSessionKeyField(_(" created with session key" ))
38+ modified_by = LastUserField(verbose_name = _(" modified by" ), related_name = " modified_%(app_label)s _%(class)s _set" )
39+ modified_with_session_key = LastSessionKeyField(_(" modified with session key" ))
40+
41+ class Meta :
42+ abstract = True
43+ ```
44+
1745
1846## Example 2 from django-cms
1947[ django-cms] ( https://github.com/divio/django-cms )
@@ -114,3 +142,86 @@ class Page(models.Model):
114142 on_delete = models.CASCADE ,
115143 )
116144```
145+
146+
147+ ## Example 3 from
148+
149+ ## Example 4 from mezzanine
150+
151+
152+ [ ** mezzanine/mezzanine/core/models.py** ] ( https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/core/models.py )
153+
154+ ``` python
155+ from __future__ import unicode_literals
156+ from future.builtins import str
157+ from future.utils import with_metaclass
158+
159+ from json import loads
160+ try :
161+ from urllib.request import urlopen
162+ from urllib.parse import urlencode
163+ except ImportError :
164+ from urllib import urlopen, urlencode
165+
166+ from django.apps import apps
167+ from django.contrib.contenttypes.fields import GenericForeignKey
168+ ~~ from django.db import models
169+ from django.db.models.base import ModelBase
170+ from django.template.defaultfilters import truncatewords_html
171+ from django.utils.encoding import python_2_unicode_compatible
172+ from django.utils.html import format_html, strip_tags
173+ from django.utils.timesince import timesince
174+ from django.utils.timezone import now
175+ from django.utils.translation import ugettext, ugettext_lazy as _
176+
177+ from mezzanine.conf import settings
178+ from mezzanine.core.fields import RichTextField, OrderField
179+ from mezzanine.core.managers import DisplayableManager, CurrentSiteManager
180+ from mezzanine.generic.fields import KeywordsField
181+ from mezzanine.utils.html import TagCloser
182+ from mezzanine.utils.models import base_concrete_model, get_user_model_name
183+ from mezzanine.utils.sites import current_site_id, current_request
184+ from mezzanine.utils.urls import admin_url, slugify, unique_slug
185+
186+
187+ user_model_name = get_user_model_name()
188+
189+
190+ def wrapped_manager (klass ):
191+ if settings.USE_MODELTRANSLATION :
192+ from modeltranslation.manager import MultilingualManager
193+
194+ class Mgr (MultilingualManager , klass ):
195+ pass
196+ return Mgr()
197+ else :
198+ return klass()
199+
200+
201+ ~~ class SiteRelated (models .Model ):
202+ """
203+ Abstract model for all things site-related. Adds a foreignkey to
204+ Django's ``Site`` model, and filters by site with all querysets.
205+ See ``mezzanine.utils.sites.current_site_id`` for implementation
206+ details.
207+ """
208+
209+ objects = wrapped_manager(CurrentSiteManager)
210+
211+ class Meta :
212+ abstract = True
213+
214+ ~~ site = models.ForeignKey(" sites.Site" , on_delete = models.CASCADE ,
215+ ~~ editable = False )
216+
217+ def save (self , update_site = False , * args , ** kwargs ):
218+ """
219+ Set the site to the current site when the record is first
220+ created, or the ``update_site`` argument is explicitly set
221+ to ``True``.
222+ """
223+ if update_site or (self .id is None and self .site_id is None ):
224+ self .site_id = current_site_id()
225+ super (SiteRelated, self ).save(* args, ** kwargs)
226+ ```
227+
0 commit comments