组件的设计问题,该组件应在保存另一条数据库记录时自动创建一条数据库记录



我有一个情况,我想在数据库中创建一个菜单项,每当一个记录被创建。
我需要设计一个组件,将创建上述菜单项。
我使用django-sitetree作为菜单的基本应用。
它有以下模型:

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> &mdash; user should have any of chosen permissions. <b>All</b> &mdash; 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...

在模型的Meta类中配置inmenu, inbreadcrumb等是否合理?
有更好的方法吗?检查是否有一个名为title的字段名,并首先将其用于菜单项的标题,然后在模型中搜索一个名为get_menu_item_title的函数,这是否合理?
或者我应该传递字段/可调用组件的构造函数?或者这也应该在元类中定义?

您可以使用信号。

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() # ..

无论何时创建新记录,您都可以创建另一个项。

相关内容

最新更新