Closed aalvrz closed 6 years ago
That's a quirk of how Django works. The field will convert to the proper object when it grabs it from the database, but it also caches it when you set it. In your example, you are setting it to a string, so it saves it in the database and caches the string object you gave it, assuming it doesn't need to do any sort of conversion. If you loaded the object fresh using .get() after the create(), you would get the proper object.
To make it work properly in the case you are going to use the object immediately without loading it fresh, you can do either of these things:
I would highly recommend simply always using the IP address/interface types to avoid this kind of thing, whether you are working with Django objects or not. Convert to/from strings only when absolutely necessary.
I am experiencing an issue where my model can have an IP field that can be a network represented by a CIDR address or a single IP.
When creating an object with just a single IP (i.e
156.25.3.189
), the object is created but the field returns a string:It would be nice if the conversion could be done to a IP Address object instead. Would this be possible?