python - Django 1.1.1: How should I store an empty IP address using PostgreSQL? -
I am writing a Django app that stores IP addresses with optional routing notifications. One of the fields I have created for the IP model is nexthop
(for next-hop routes), which will generally empty, basically we use MySQL, but now change the project requirements Intended to use PostgreSQL
Here is a stripped version of your model:.
class.model: address = models.IPAddressField () netmask = models.IPAddressField (default = '255.255.255.255') nexthop = models.IPAddressField (zero = true, empty = True, default = none) Active = models.BooleanField ('active' ?, default = 1)
Then, with the MySQL, the nexthop
field There was no problem leaving the blank. However, now that I have changed the development environment to postgres, we run it in which a blank IP address raises invalid input syntax for a DataError
As you can see, this bomb because it is trying to insert an empty string when the column will only accept NULL
.
I have a very real need to be able to keep this area empty, because if there is no next hop in the IP, then its behavior changes.
The lack of hacking Django code manually, which is my last last resort, I thought hop next to 255.255.255.255 and wrapping some business logic around that (ie if the next hop 255.255 255.255, treated as a normal route), but it seems like just a hack.
I will know that there is a better way to do that, Woo
Thanks in advance!
Edit: Interim solution: There is no need to write DDO, hacking or hacking logic, or if there is a completely different approach that can meet my requirement.
For the time (as an interim improvement) I decided to go with the sensor value for the next hop:
In the model:
IP_NEXTHOP_SENTINEL = '255.255.255.255' class.model: nexthop = models.IPAddressField (zero = true, empty = true, default = IP_NEXTHOP_SENTINEL, help_text = 'Use% s to indicate next -hop'% IP_NEXTHOP_SENTINEL) def save (self, * args, ** kwargs): Django # 5622 (http://code.djangoproject.com/ticket/5622) If hack for self.nexthop and self.nexthop ## = = IP_NEXTHOP_SENTINEL: self.nexthop = none
Overview:
creation IP
is intended as the object of objects outside the portal portal, which is why i null = true
nexthop
. The only place where 255.255.255.255 will be set as the next-hop, will be through the administration portal, so I have decided that for the overload of save
None
will give me the final result which I want and does not really feel much like a hack.
Thanks for your input on this!
If you accept devs from one of the patch, then I say that the foot version Run a copy copy of Django to the ground. If not, it is a head, even if it is suggested, then it may be less headache to use only a sentinel value. You can also use a regular CharField
instead of a IPAddressField
, but then you are stuck to maintaining your own verification logic.
Comments
Post a Comment