File: managers.py

package info (click to toggle)
python-django-netfields 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 444 kB
  • sloc: python: 2,067; sh: 7; makefile: 4; sql: 2
file content (19 lines) | stat: -rw-r--r-- 591 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from django.db import models
from ipaddress import _BaseNetwork

try:
    str_type = unicode
except NameError:
    str_type = str


class NetManager(models.Manager):
    use_for_related_fields = True

    def filter(self, *args, **kwargs):
        for key, val in kwargs.items():
            if isinstance(val, _BaseNetwork):
                # Django will attempt to consume the _BaseNetwork iterator, which
                # will convert it to a list of every address in the network
                kwargs[key] = str_type(val)
        return super(NetManager, self).filter(*args, **kwargs)