Django: required together?

Do you know this:

unique_together = ("name", "date") 

Is there something similar for required fields?

I have 2 fields: ipv4 and ipv6. There are different wireless networks on the map, we call them β€œislands” because they are not physically connected, but through a VPN tunnel.

Some islands use ipv4, and they implement ipv6, while others only use ipv6. If I installed ipv4 to the required one, it would be problematic for those that are only ipv6, and if I install ipv6 to the required ones, then basically ipv4 will have problems.

There are two things that I could do: set both fields as not required or set them in such a way that at least one of them is filled.

The first solution is easy, but not very pleasant, and the second is pleasant, but I do not know if this is possible without hacking django.

The application is open source.

Source: https://github.com/ninuxorg/nodeshot/

Demo: http://map.ninux.org

+7
source share
1 answer

You can write a clean method for your model. This will be called whenever you clear the model form, including the django admin.

 from django.core.exceptions import ValidationError class MyModel(model.Model): <field definitions> def clean(self): """ Require at least one of ipv4 or ipv6 to be set """ if not (self.ipv4 or self.ipv6): raise ValidationError("An ipv4 or ipv6 address is required") 

For more information, see the Object Inspection documents.

+7
source

All Articles