Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to add custom encoder for ipv4 #7

Closed
avaldevilap opened this issue Oct 20, 2020 · 2 comments · Fixed by #37
Closed

Need to add custom encoder for ipv4 #7

avaldevilap opened this issue Oct 20, 2020 · 2 comments · Fixed by #37
Labels
bug Something isn't working
Projects

Comments

@avaldevilap
Copy link

How can i do this?

class Gateway(Model):
    serial_number: str
    name: str
    ip: IPv4Address
    devices: Optional[List[PeripheralDvice]] = Field(max_items=10)

    class Config:
        json_encoders = {IPv4Address: lambda v: str(v)}
@art049
Copy link
Owner

art049 commented Oct 20, 2020

Normally, since Pydantic already handle this field type, you shouldn't have to specify those json_encoders.

from odmantic import Model
from ipaddress import IPv4Address

class Gateway(Model):
    ip: IPv4Address

g = Gateway(ip=IPv4Address("192.168.0.1"))
assert g.json() == '{"ip": "192.168.0.1", "id": "..."}'

However, if you need custom json_encoders, you're right. It's currently not working. I'll release a patch soon.

@art049 art049 added the bug Something isn't working label Oct 20, 2020
@art049
Copy link
Owner

art049 commented Oct 22, 2020

Might be related to pydantic/pydantic#2024

@art049 art049 added this to Next release in Development via automation Oct 26, 2020
@art049 art049 closed this as completed in #37 Nov 8, 2020
Development automation moved this from Next release to Done Nov 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development
  
Done
Development

Successfully merging a pull request may close this issue.

2 participants