|
| 1 | +title: django.db.models.Model Examples |
| 2 | +category: page |
| 3 | +slug: django-db-models-model-examples |
| 4 | +sortorder: 50002 |
| 5 | +toc: False |
| 6 | +sidebartitle: django.db.models.Model Examples |
| 7 | +meta: Python code examples for the Model class within the django.db.models module of the Django project. |
| 8 | + |
| 9 | + |
| 10 | +# django.db.models.Model Examples |
| 11 | +The |
| 12 | +[Model](https://github.com/django/django/blob/master/django/db/models/base.py) |
| 13 | +class is the superclass for all data stored in [Django](/django.html) |
| 14 | +applications. |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## Example 2 from django-cms |
| 19 | +[django-cms](https://github.com/divio/django-cms) |
| 20 | +([project website](https://www.django-cms.org/en/)) is a Python-based |
| 21 | +content management system (CMS) |
| 22 | +[code library](https://pypi.org/project/django-cms/) |
| 23 | +for use with Django web apps that is open sourced under the |
| 24 | +[BSD 3-Clause "New" License](https://github.com/divio/django-cms/blob/develop/LICENSE). |
| 25 | + |
| 26 | +```python |
| 27 | +@python_2_unicode_compatible |
| 28 | +class Page(models.Model): |
| 29 | + """ |
| 30 | + A simple hierarchical page model |
| 31 | + """ |
| 32 | + LIMIT_VISIBILITY_IN_MENU_CHOICES = ( |
| 33 | + (constants.VISIBILITY_USERS, _('for logged in users only')), |
| 34 | + (constants.VISIBILITY_ANONYMOUS, _('for anonymous users only')), |
| 35 | + ) |
| 36 | + TEMPLATE_DEFAULT = TEMPLATE_INHERITANCE_MAGIC if get_cms_setting('TEMPLATE_INHERITANCE') else get_cms_setting('TEMPLATES')[0][0] |
| 37 | + |
| 38 | + X_FRAME_OPTIONS_INHERIT = constants.X_FRAME_OPTIONS_INHERIT |
| 39 | + X_FRAME_OPTIONS_DENY = constants.X_FRAME_OPTIONS_DENY |
| 40 | + X_FRAME_OPTIONS_SAMEORIGIN = constants.X_FRAME_OPTIONS_SAMEORIGIN |
| 41 | + X_FRAME_OPTIONS_ALLOW = constants.X_FRAME_OPTIONS_ALLOW |
| 42 | + X_FRAME_OPTIONS_CHOICES = ( |
| 43 | + (constants.X_FRAME_OPTIONS_INHERIT, _('Inherit from parent page')), |
| 44 | + (constants.X_FRAME_OPTIONS_DENY, _('Deny')), |
| 45 | + (constants.X_FRAME_OPTIONS_SAMEORIGIN, _('Only this website')), |
| 46 | + (constants.X_FRAME_OPTIONS_ALLOW, _('Allow')) |
| 47 | + ) |
| 48 | + |
| 49 | + template_choices = [(x, _(y)) for x, y in get_cms_setting('TEMPLATES')] |
| 50 | + |
| 51 | + created_by = models.CharField( |
| 52 | + _("created by"), max_length=constants.PAGE_USERNAME_MAX_LENGTH, |
| 53 | + editable=False) |
| 54 | + changed_by = models.CharField( |
| 55 | + _("changed by"), max_length=constants.PAGE_USERNAME_MAX_LENGTH, |
| 56 | + editable=False) |
| 57 | + creation_date = models.DateTimeField(auto_now_add=True) |
| 58 | + changed_date = models.DateTimeField(auto_now=True) |
| 59 | + |
| 60 | + publication_date = models.DateTimeField(_("publication date"), null=True, blank=True, help_text=_( |
| 61 | + 'When the page should go live. Status must be "Published" for page to go live.'), db_index=True) |
| 62 | + publication_end_date = models.DateTimeField(_("publication end date"), null=True, blank=True, |
| 63 | + help_text=_('When to expire the page. Leave empty to never expire.'), |
| 64 | + db_index=True) |
| 65 | + # |
| 66 | + # Please use toggle_in_navigation() instead of affecting this property |
| 67 | + # directly so that the cms page cache can be invalidated as appropriate. |
| 68 | + # |
| 69 | + in_navigation = models.BooleanField(_("in navigation"), default=True, db_index=True) |
| 70 | + soft_root = models.BooleanField(_("soft root"), db_index=True, default=False, |
| 71 | + help_text=_("All ancestors will not be displayed in the navigation")) |
| 72 | + reverse_id = models.CharField(_("id"), max_length=40, db_index=True, blank=True, null=True, help_text=_( |
| 73 | + "A unique identifier that is used with the page_url templatetag for linking to this page")) |
| 74 | + navigation_extenders = models.CharField(_("attached menu"), max_length=80, db_index=True, blank=True, null=True) |
| 75 | + template = models.CharField(_("template"), max_length=100, choices=template_choices, |
| 76 | + help_text=_('The template used to render the content.'), |
| 77 | + default=TEMPLATE_DEFAULT) |
| 78 | + |
| 79 | + login_required = models.BooleanField(_("login required"), default=False) |
| 80 | + limit_visibility_in_menu = models.SmallIntegerField(_("menu visibility"), default=None, null=True, blank=True, |
| 81 | + choices=LIMIT_VISIBILITY_IN_MENU_CHOICES, db_index=True, |
| 82 | + help_text=_("limit when this page is visible in the menu")) |
| 83 | + is_home = models.BooleanField(editable=False, db_index=True, default=False) |
| 84 | + application_urls = models.CharField(_('application'), max_length=200, blank=True, null=True, db_index=True) |
| 85 | + application_namespace = models.CharField(_('application instance name'), max_length=200, blank=True, null=True) |
| 86 | + |
| 87 | + # Placeholders (plugins) |
| 88 | + placeholders = models.ManyToManyField('cms.Placeholder', editable=False) |
| 89 | + |
| 90 | + # Publisher fields |
| 91 | + publisher_is_draft = models.BooleanField(default=True, editable=False, db_index=True) |
| 92 | + # This is misnamed - the one-to-one relation is populated on both ends |
| 93 | + publisher_public = models.OneToOneField( |
| 94 | + 'self', |
| 95 | + on_delete=models.CASCADE, |
| 96 | + related_name='publisher_draft', |
| 97 | + null=True, |
| 98 | + editable=False, |
| 99 | + ) |
| 100 | + languages = models.CharField(max_length=255, editable=False, blank=True, null=True) |
| 101 | + |
| 102 | + # X Frame Options for clickjacking protection |
| 103 | + xframe_options = models.IntegerField( |
| 104 | + choices=X_FRAME_OPTIONS_CHOICES, |
| 105 | + default=get_cms_setting('DEFAULT_X_FRAME_OPTIONS'), |
| 106 | + ) |
| 107 | + |
| 108 | + # Flag that marks a page as page-type |
| 109 | + is_page_type = models.BooleanField(default=False) |
| 110 | + |
| 111 | + node = models.ForeignKey( |
| 112 | + TreeNode, |
| 113 | + related_name='cms_pages', |
| 114 | + on_delete=models.CASCADE, |
| 115 | + ) |
| 116 | +``` |
0 commit comments