Skip to content

Fix to avoid NoneType error. #1

@DasJennir

Description

@DasJennir

Some people may encounter the error of 'NoneType' object has no attribute 'city_set' if you implement this to a profile system or any other similar model. My way around to fix it was :

   if 'country' in self.data:
        try:
            country_id = int(self.data.get('country'))
            self.fields['city'].queryset = City.objects.filter(country_id=country_id).order_by('name')
        except (ValueError, TypeError):
            pass  # invalid input from the client; ignore and fallback to empty City queryset
    elif self.instance.pk:
          try:
              self.fields['city'].queryset = self.instance.country.city_set.order_by('name')
          else:
              pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions