Design problem with a component that should automatically create a database record when another one is saved
I have a situation where I want to create a menu item in the database whenever a record is created.
I need to design a component that will create the mentioned menu item. I am using django-sitetree as the basic app for the menu. It has the following model:class TreeItem(models.Model):
PERM_TYPE_ANY = 1
PERM_TYPE_ALL = 2
PERM_TYPE_CHOICES = (
(PERM_TYPE_ANY, _('Any')),
(PERM_TYPE_ALL, _('All'))
)
title = models.CharField(_('Title'), max_length=100, help_text=_('Site tree item title. Can contain template variables E.g.: {{ mytitle }}.'))
hint = models.CharField(_('Hint'), max_length=200, help_text=_('Some additional information about this item that is used as a hint.'), blank=True, default='')
url = models.CharField(_('URL'), max_length=200, help_text=_('Exact URL or URL pattern (see "Additional settings") for this item.'), db_index=True)
urlaspattern = models.BooleanField(_('URL as Pattern'), help_text=_('Whether the given URL should be treated as a pattern.<br /><b>Note:</b> Refer to Django "URL dispatcher" documentation (e.g. "Naming URL patterns" part).'), db_index=True, default=False)
tree = models.ForeignKey(Tree, verbose_name=_('Site Tree'), help_text=_('Site tree this item belongs to.'), db_index=True)
hidden = models.BooleanField(_('Hidden'), help_text=_('Whether to show this item in navigation.'), db_index=True, default=False)
alias = CharFieldNullable(_('Alias'), max_length=80, help_text=_('Short name to address site tree item from a template.<br /><b>Reserved aliases:</b> "trunk", "this-children", "this-siblings" and "this-ancestor-children".'), db_index=True, blank=True, null=True)
description = models.TextField(_('Description'), help_text=_('Additional comments on this item.'), blank=True, default='')
inmenu = models.BooleanField(_('Show in menu'), help_text=_('Whether to show this item in a menu.'), db_index=True, default=True)
inbreadcrumbs = models.BooleanField(_('Show in breadcrumb path'), help_text=_('Whether to show this item in a breadcrumb path.'), db_index=True, default=True)
insitetree = models.BooleanField(_('Show in site tree'), help_text=_('Whether to show this item in a site tree.'), db_index=True, default=True)
access_loggedin = models.BooleanField(_('Logged in only'), help_text=_('Check it to grant access to this item to authenticated users only.'), db_index=True, default=False)
access_restricted = models.BooleanField(_('Restrict access to permissions'), help_text=_('Check it to restrict user access to this item, using Django permissions system.'), db_index=True, default=False)
access_permissions = models.ManyToManyField(Permission, verbose_name=_('Permissions granting access'), blank=True)
access_perm_type = models.IntegerField(_('Permissions interpretation'), help_text='<b>Any</b> — user should have any of chosen permissions. <b>All</b> — user should have all chosen permissions.', choices=PERM_TYPE_CHOICES, default=PERM_TYPE_ANY)
# These two are for 'adjacency list' model.
# This is the current approach of tree representation for sitetree.
parent = models.ForeignKey('self', verbose_name=_('Parent'), help_text=_('Parent site tree item.'), db_index=True, null=True, blank=True)
sort_order = models.IntegerField(_('Sort order'), help_text=_('Item position among other site tree items under the same parent.'), db_index=True,开发者_如何转开发 default=0)
# More code here...
Is it reasonable to configure inmenu, inbreadcrumbs etc. in the model's Meta class?
Is there a better way to do this? Is it reasonable to check if a there is field name called title and use it for the menu item's title first and then search for a function called get_menu_item_title in the model? Or should I pass the field/callable to the component's constructor? Or maybe this should be defined in the meta class as well?You can use Signals.
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import Record, TreeItem
@receiver(post_save, sender=Record)
def my_handler(sender, instance, created, raw):
if created:
item = TreeItem() # ..
Whenever new record is created you can create another item.
精彩评论