我有两个应用程序news
,article
它们都具有完全相同的型号名称Comment
:
class Comment(models.Model): author = models.ForeignKey(User) created = models.DateTimeField(auto_now_add=True) title = models.CharField(max_length=100, default='', blank=True) body = models.TextField() post = models.ForeignKey(Photo) published = models.BooleanField(default=True)
现在,在一个视图中我想删除两个应用程序中的某些注释:
Comment.objects.filter(author=someauthor).delete()
如何在不更改模型名称的情况下实现这一目标?
您可以使用import ... as ...
这两个模型名称不冲突:
from news.models import Comment as NewsComment from article.models import Comment as ArticleComment ... NewsComment.objects.filter(author=someauthor).delete() ArticleComment.objects.filter(author=someauthor).delete()