supermario
supermario

Reputation: 2755

How to delete objects from two apps with the same model name?

I have two apps news and article which both have exactly the same model name 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)

Now, in a view I want to delete certain comments from both apps:

Comment.objects.filter(author=someauthor).delete()

How can I achieve that without changing the model names?

Upvotes: 2

Views: 52

Answers (1)

falsetru
falsetru

Reputation: 369064

You can use import ... as ... so that both model names do not conflict:

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

Upvotes: 8

Related Questions