Django: get all unflagged comments for a Model

I recently had to migrate django.contrib.comments from a legacy app to a newer app, also in Django. I only wanted to move relevant comments that were unflagged (no need to duplicate spam!)

Here’s how I did it (manage.py shell usage):

from django.contrib.comments.models import Comment
from django.db.models import Count
 
from legacy.models import LegacyArticle
 
 
las = LegacyArticle.objects.all_published()
 
for la in las:
    comments = Comment.objects.for_model(LegacyArticle).annotate(flagcount=Count('flags')).filter(object_pk=la.pk, flagcount=0)
    # handler code...

Django: handle form validation with combined POST and FILES data

If you have a Django forms.Form with two or more fields to validate that require information from each other you can test these in the forms’ clean method.

However, if you need to test a condition that relies upon normal form data in addition to uploaded file data, the code below will let you do so; or see my Gist on GitHub.