def update_population_density(geounit_set):
    subject, created = Subject.objects.get_or_create(name='Population Density')
    for geounit in geounit_set:
        total_population = geounit.statistic_set.get(subject__name='Total Population').value
        area = geounit.area_sq_mi()
        stat = Statistic(subject=subject, geounit=geounit, value=total_population / area)
        stat.save()
    def handle(self, *args, **options):
        subject, created = Subject.objects.get_or_create(name=options["subject"])

        for csvfile in args:
            subject_reader = csv.DictReader(open(csvfile, "rb"))
            for row in subject_reader:
                try:
                    block = CensusBlock.objects.get(geoid10=row[options["geoidfield"]].strip())
                    stat = Statistic(subject=subject, geounit=block, value=row[options["valuefield"]])
                    sys.stderr.write("Setting %s for %s to %s\n" % (subject.name, stat.geounit.geoid10, stat.value))
                    stat.save()

                except ObjectDoesNotExist:
                    # Some blocks might not exist in our system since we're
                    # likely only loading ones in the cities.
                    sys.stderr.write(
                        "Block with GEOID10 %s not found in system\n" % (row[options["geoidfield"]].strip())
                    )