Issue: Document peewee 3 change of `register_fields`.
Author: kdart
State: closed
Created: 2018-02-04 03:31:22

I'm just getting around to updating my project to use peewee 3. I got most of what is in the differences page, but it doesn't mention `register_fields` class method of PostgresExtDatabase is also now missing. How should I handle that?

It looks like you now pass in the `field_types` keyword argument when first creating the database. Is that right? 

Trying the above might work, but now I'm seeing the following when trying to import my models.

    # OUT: Traceback (most recent call last):
    # OUT:   File "<input>", line 1, in <module>
    # OUT:     from devtest.db import models
    # OUT:   File "/home/keith/src/devtest/devtest/db/models.py", line 81, in <module>
    # OUT:     class EquipmentModel(BaseModel):
    # OUT:   File "/usr/local/lib/python3.6/site-packages/peewee.py", line 4888, in __new__
    # OUT:     cls._meta.add_field(name, field)
    # OUT:   File "/usr/local/lib/python3.6/site-packages/peewee.py", line 4689, in add_field
    # OUT:     field.bind(self.model, field_name, set_attribute)
    # OUT:   File "/usr/local/lib/python3.6/site-packages/peewee.py", line 3750, in bind
    # OUT:     self._constructor = model._meta.database.get_binary_type()
    # OUT:   File "/usr/local/lib/python3.6/site-packages/peewee.py", line 388, in __getattr__
    # OUT:     raise AttributeError('Cannot use uninitialized Proxy.')
    # OUT: AttributeError: Cannot use uninitialized Proxy.


Thanks for reporting. What kind of special field-types are you trying to register?

It looks like that traceback you posted is a result of a `BlobField` trying to interrogate the database object for it's "binary_type". Since the db isn't initialized, the lookup fails. Let me see about a fix.

Regarding custom field types, yep, you pass a dict mapping field type to db-specific data-type.

You might try this commit: 32d9ca7787e8634aba63f216bb7993160f34fc51

I've pushed a new release, 3.0.11, if you're interested.

Thanks!  I have custom fields for Postgres, some map to `inet`, `cidr`, and `macaddr` special fields. 

In case you're interested in the custom fields:

https://github.com/kdart/devtest/blob/master/devtest/db/fields.py



Kool, thanks for sharing!

Hi! 

Could you please share how-to declare a `cidr` field in a peewee model ?

BTW, the link above handles only IPv4 networks if I am not mistaken.

Probably depends on your database driver. Postgres has a native data-type for CIDR network. You could probably use an integer column and the Python 3 `ipaddress` module:

```python
import ipaddress
net = ipaddress.ip_network('192.168.1.0/28')
netmask = net.netmask
four_bytes = netmask.packed
to_int = struct.unpack('!I', four_bytes)[0]
```

actually I was wondering if there is sth similar to `JSONField`, `ArrayField`, etc. that I could use in the case of type `CIDR`:

```
from peewee import *
from playhouse.postgres_ext import PostgresqlExtDatabase, ArrayField, JSONField
import psycopg2
import psycopg2.extras

class Interconnect(BaseModel):
    as_set = ArrayField(field_class=TextField)
    pulse = JSONField(null=True)
    prefix = CIDRField()
    ...
```

where `prefix` in PostgreSQL is defined already as:
```
 prefix        CIDR NOT NULL,
```

@coleifer I just upgraded to peewee 3 and don't see documentation for the missing `register_fields` and `register_ops`. Could some documentation be added on how to proceed? I believe, as kdart mentioned, that there is no classmethod anymore, but we can use `field_types` and `register_function` on the instance. Is that right?

You don't need to register anything anymore.

```python
class MyMagicalField(Field):
    field_type = 'column_data_type'

    def special_field_expression(self, value):
        return Expression(self, 'operator', value)
```