palewire / django-calaccess-campaign-browser

A Django app to refine, review and republish campaign finance data drawn from the California Secretary of State’s CAL-ACCESS database
http://django-calaccess-campaign-browser.californiacivicdata.org
MIT License
17 stars 12 forks source link

Expenditure model `to_dict` throws DoesNotExist error #196

Closed aboutaaron closed 9 years ago

aboutaaron commented 9 years ago

Problem

In [18]: from calaccess_campaign_browser.models import Expenditure

In [19]: e = Expenditure.objects.all()[0]

In [20]: e.to_dict()
---------------------------------------------------------------------------
DoesNotExist                              Traceback (most recent call last)
<ipython-input-20-d737237496c7> in <module>()
----> 1 e.to_dict()

/home/aaron/Code/california-civic-data-coalition/django-calaccess-campaign-browser/calaccess_campaign_browser/utils/models.pyc in to_dict(self)
     24         d = SortedDict({})
     25         for f in self._meta.fields:
---> 26             d[f.verbose_name] = getattr(self, f.name)
     27         return d
     28 

/home/aaron/.envs/campaign_finance/local/lib/python2.7/site-packages/django/db/models/fields/related.pyc in __get__(self, instance, instance_type)
    570                     qs = qs.filter(extra_filter, **params)
    571                 # Assuming the database enforces foreign keys, this won't fail.
--> 572                 rel_obj = qs.get()
    573                 if not self.field.rel.multiple:
    574                     setattr(rel_obj, self.field.related.get_cache_name(), instance)

/home/aaron/.envs/campaign_finance/local/lib/python2.7/site-packages/django/db/models/query.pyc in get(self, *args, **kwargs)
    355             raise self.model.DoesNotExist(
    356                 "%s matching query does not exist." %
--> 357                 self.model._meta.object_name)
    358         raise self.model.MultipleObjectsReturned(
    359             "get() returned more than one %s -- it returned %s!" % (

DoesNotExist: Committee matching query does not exist.
aboutaaron commented 9 years ago

Reason

to_dict() fails because there's an issue with Expenditure.candidate_committee. Looks like the foreign key relationship isn't setup correctly. See calaccess_campaign_browser/models/expenditures.py#L45

Looks like something doesn't work during the load on calaccess_campaign_browser/management/commands/loadcalaccesscampaignexpenditures.py#L230