vnoelifant / charity-finder

3 stars 0 forks source link

18 countries model #21

Closed vnoelifant closed 1 year ago

vnoelifant commented 1 year ago

Didn't really have time to debug this, but I am getting this error after trying to seed the countries dictionary from the organization data:

(charity-finder) ronnienm08@DESKTOP-94KD1T3:~/charity-finder$ python manage.py import_organizations --model org
Seeding organization data
Traceback (most recent call last):
  File "/home/ronnienm08/charity-finder/manage.py", line 22, in <module>
    main()
  File "/home/ronnienm08/charity-finder/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/home/ronnienm08/charity-finder/charity_finder/management/commands/import_organizations.py", line 82, in handle
    insert_active_orgs()
  File "/home/ronnienm08/charity-finder/charity_finder/management/commands/import_organizations.py", line 60, in insert_active_orgs
    matching_themes = get_matching_data(themes_from_json)
  File "/home/ronnienm08/charity-finder/charity_finder/management/commands/import_organizations.py", line 17, in get_matching_data
    else Theme.objects.get_or_create(
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/db/models/query.py", line 928, in get_or_create
    return self.get(**kwargs), False
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/db/models/query.py", line 636, in get
    clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/db/models/query.py", line 1420, in filter
    return self._filter_or_exclude(False, args, kwargs)
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/db/models/query.py", line 1438, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, args, kwargs)
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/db/models/query.py", line 1445, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1532, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1562, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1407, in build_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg)
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1217, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/home/ronnienm08/.local/share/virtualenvs/charity-finder-eKohszMN/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1709, in names_to_path
    raise FieldError(
django.core.exceptions.FieldError: Cannot resolve keyword 'country_code' into field. Choices are: id, name, theme_id, themes
(charity-finder) ronnienm08@DESKTOP-94KD1T3:~/charity-finder$ 

Also for some reason in my admin I still see the plural display of Country model as "Countrys" even if I added the following in models.py:

        class Meta:
            verbose_name_plural = "countries"

Please let me know your comments/refactoring suggestions. Thank you!

bbelderbos commented 1 year ago

@vnoelifant seems you're access country_code on Theme, but it's an attribute of Country. This happens in the command script:

            else Theme.objects.get_or_create(
                name=row.get("name", ""), country_code=row.get("iso3166CountryCode", "")
            )
vnoelifant commented 1 year ago

Closing this with following notes: Latest commit: https://github.com/vnoelifant/charity-finder/pull/21/commits/73994d91cc43a340da8512d4c60d3b5e58af3e00 1) Burma Humanitarian Mission shows themes and countries data but the original JSON orgs file (that I sent you in Slack) shows that these fields should be Null. What could be the cause of this? I realize that I use the continue statement in import_organizations.py if themes and countries is None so I thought maybe it relates to that, and writing data that is not needed, and skipping over writing data that is needed. Would I need to use a generator maybe? 2) There is duplicate code in lines 47-90 in import_organizations.py . Any suggestions to wrap this all into a function?

bbelderbos commented 1 year ago

Closing this with following notes: Latest commit: 73994d9

  1. Burma Humanitarian Mission shows themes and countries data but the original JSON orgs file (that I sent you in Slack) shows that these fields should be Null. What could be the cause of this? I realize that I use the continue statement in import_organizations.py if themes and countries is None so I thought maybe it relates to that, and writing data that is not needed, and skipping over writing data that is needed. Would I need to use a generator maybe?
  2. There is duplicate code in lines 47-90 in import_organizations.py . Any suggestions to wrap this all into a function?

Happy to look at this today on our call ...