django ã§è«çåé¤ãå®ç¾ãã¦ã¿ã
ã¤ãã³ãç³»ã®ãã¼ã¿ãªãã¨ãããããªã½ã¼ã¹ç³»ã®ãã¼ã¿ã®è«çåé¤ã¨ããã®ã¯çµæ§éè¦ãå¤ãããªæ°ããããã©ããã¾ã触ãããã¦ãªãã®ã¯ãªãã§ã ãããï¼ãã§ã« Django Snippets ã¨ãã«ãããããªæ°ããããã©ããªããªãããã¢ã¤ãã¢ãæµ®ããã ã®ã§ã¡ã¢ãã¦ããã
ã¾ãã¯åæã¨ããã¢ãã«ããã
class Author(models.Model): name = models.CharField(u'åå', max_length=255) deleted = models.BooleanField(u'è«çåé¤ãã©ã°', default=False) def delete(self): self.deleted = True self.save() class Entry(models.Model): content = models.TextField(u'å 容') author = models.ForeignKey(Author, verbose_name=u'ä½è ') deleted = models.BooleanField(u'è«çåé¤ãã©ã°', default=False) def delete(self): self.deleted = True self.save()
ç¹ã«ç¹å¥ãªãã¨ã¯ãã£ã¦ãªããè«çåé¤ãå®ç¾ããã¨ãã«ããæãã¤ãå½¢ã ã¨æããã§ãæå¹ãªãã¼ã¿ãåå¾ããããã°ãAuthor.objects.filter(deleted=False) ã¨ããããããã§ãä¸å¿å®ç¾ã§ãã¦ããã ãã©ããã¾ãã¡é¢åãããããã³ãã¬ã¼ãã§ã¯ãã¼ã¯ã¼ãå¼æ°ã使ããªããã
ããã解決ããããã«ãã«ã¹ã¿ã ããã¸ã£ã¨ãããã®ãç¨æããã¦ããã
åºåºã¯ã©ã¹ã® Manager ã¯ã©ã¹ãæ¡å¼µãã¦ãã¢ãã«ä¸ã§ã«ã¹ã¿ã ã®ããã¸ã£ãã¤ã³ã¹ã¿ã³ã¹åããã°ãã¢ãã«ã§ã«ã¹ã¿ã ã®ããã¸ã£ã使ãã¾ãã
ããã¸ã£ãã«ã¹ã¿ãã¤ãºããçç±ã¯å¤§ããåãã¦äºã¤ããã¾ããä¸ã¤ã¯ããã¸ã£ã«è¿½å ã®ã¡ã½ãããæããããå ´åãããä¸ã¤ã¯ããã¸ã£ã®è¿ãåæ QuerySet ãå¤æ´ãããå ´åã§ãã
http://michilu.com/django/doc-ja/model-api/#id31
ãããªæãã«å®ç¾©ãã¦ããã°ãEntry.objects.all() ã§æå¹ãªãã¼ã¿ãåå¾ã§ããã
class PublicManager(models.Manager): def get_query_set(self): return super(PublicManager, self).get_query_set().filter(deleted=False) class Entry(models.Model): content = models.TextField(u'å 容') author = models.ForeignKey(Author, verbose_name=u'ä½è ') deleted = models.BooleanField(u'è«çåé¤ãã©ã°', default=False) objects = PublicManager()
æ®éã«ä½¿ãåã«ã¯ããã§ååã ã¨æãããã ãã管çãµã¤ãã§å°ã£ããã¨ã«ãªããè«çåé¤ãããã¼ã¿ãæ®éã«è¦ããããã ããã¯ãªãã¯ãã¦ç·¨éãããã¨ãã¦ã404ãã¼ã¸ã«é£ã°ããããã¤ã¾ããä¸åº¦è«çåé¤ããããmanage.py shell ããå¤æ´ãããDBãç´æ¥ç·¨éãããã復活ãããæ段ããªããä¸å¿çã«ããªã£ãä»æ§ãã¨æããã©ããã£ã±ãããã©ãããã管çãµã¤ãã§å¾©æ´»ã§ããæ¹ã楽ã ã
ããã§ãã«ã¹ã¿ã ããã¸ã£ã¯è¤æ°æã¤ãã¨ãã§ããã®ãå©ç¨ãã¦ããããªé¢¨ã«å®ç¾©ãã¦ã¿ãã
class Entry(models.Model): content = models.TextField(u'å 容') author = models.ForeignKey(Author, verbose_name=u'ä½è ') deleted = models.BooleanField(u'è«çåé¤ãã©ã°', default=False) objects = models.Manager() public_objects = PublicManager()
ããããã¨ç®¡çãµã¤ããã復活ã§ããããEntry.public_objects.all() ã§æå¹ãã¼ã¿ãåå¾ã§ããã
ããã§ãã§ãããã¨æãããããªã¬ã¼ã·ã§ã³ãéã«ãã©ãå ´åã«å°ã£ããã¨ã«ãªãã django ã¯æåã«å®ç¾©ãã¦ããããã¸ã£ã使ãã¿ãããªã®ã§ãauthor.entry_set.all() ã¨ãããã¨ãè«çåé¤ãããã¼ã¿ãåå¾ã§ãã¦ãã¾ããããã¯ãã¾ããããããªãã Author ããã¼ã«ã㦠Entry ã表示ãããã¼ã¸ã表示ããããã ãã«ããã¥ã¼ã§ author.entry_set.filter(deleted=False) ã¨ãããã®ã¯ããã«ãããããªãã
ä»ã¾ã§ã¯ããã¾ã§ã§è«¦ãã¦ãã¥ã¼ã§è¦å´ãã¦ããã ãã©ãã¢ãã«APIãªãã¡ã¬ã³ã¹ãèªã¿ç´ãã¦ãã¦ãµã¨æãã¤ããã
ã«ã¹ã¿ã ã®ããã¸ã£ã¡ã½ããã¯ä½ãè¿ãã¦ããã¾ãã¾ããã QuerySet ãè¿ããªãã¦ãããã®ã§ãã
http://michilu.com/django/doc-ja/model-api/
ã¤ã¾ãå¥ã® QuerySet ãè¿ãã¦ãåé¡ãªããã¨ããããã§ãããªé¢¨ã«å®ç¾©ãã¦ã¿ãã
class PublicManager(models.Manager): def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs def get_query_set(self): return super(PublicManager, self).get_query_set() def public_all(self): return self.get_query_set().filter(*self.args, **self.kwargs) def public_filter(self, *args, **kwargs): return self.public_all().filter(*args, **kwargs) class Entry(models.Model): content = models.TextField(u'å 容') author = models.ForeignKey(Author, verbose_name=u'ä½è ') deleted = models.BooleanField(u'è«çåé¤ãã©ã°', default=False) objects = PublicManager(deleted=False)
ããããã¨ãä»ã¾ã§ä¸ãã¦ããåé¡ã¯ã»ã¨ãã©è§£æ±ºã§ããã
- Entry.objects.all() ã¯å ¨ãã¼ã¿ãè¿ãã
- Entry.objects.public_all() ã¯æå¹ãã¼ã¿ã ããè¿ãã
- Entry.objects.public_filter() ã¯æå¹ãã¼ã¿ããã£ã«ã¿ããçµæãè¿ãã
- author.entry_set.all() ã¯ã author ã«é¢é£ããå ¨ãã¼ã¿ãè¿ãã
- author.entry_set.public_all() ã¯ã author ã«é¢é£ããæå¹ãã¼ã¿ãè¿ãã
管çãµã¤ã㧠404 ãã¼ã¸ã«é£ã°ããããã¨ããªãããã©ããã£ã¦ãã³ãã¬ã¼ãã« author.entry_set.filter(deleted=False) ã渡ããæ©ãå¿ è¦ãããã¾ããã
- 追è¨
- 管çãµã¤ãã§ãEntryããé¸æã§ããAuthorãå¶éããããªããauthor = models.ForeignKey(Author, limit_choices_to = {'deleted': False}) ã¨ãããã°ããã
ãªãã§æ°ãä»ããªãã£ããã ããã»ã»ã»