models.py:
class Hosts (models.Model) :
host_label = models.CharField (max_length=64,
verbose_name = "Host Label"
)
host_activation_request = models.CharField (max_length = 400,
verbose_name = 'Activation Request',
blank=True, null=True)
host_developer_email = models.CharField (max_length = 400,
verbose_name = 'Developer Email',
blank=True, null=True)
class Meta:
verbose_name = "Available Host"
unique_together = [['host_label', 'host_developer_email']]
serializers.py:
class HostSerializer(serializers.ModelSerializer):
class Meta:
model = Hosts
fields = ('host_label','host_activation_request')
views.py:
class CreateHost(CreateAPIView):
serializer_class = HostSerializer
def generate_activation_request(self, feature, customer_ref="Isode"):
config = get_active_config() #this function has been imported at the top
command_list = [os.path.join(config.generate_activation_binary_directory, "generate_activation_request")]
import subprocess
proc = subprocess.run(command_list, universal_newlines=True, capture_output=True)
self.result = proc.stdout
def post(self, request, *args, **kwargs):
print(request.data)
self.generate_activation_request(feature=request.data["feature"])
request.data['host_activation_request'] = self.result
return self.create(request, *args, **kwargs)
A user can login to the website and make a Host in there and the host_developer_email field will be their email and the other two fields will be whatever they input in the form, as host_developer_email has been set to be a hidden field.
Now, I have created an endpoint called /api/host which I can send a post request to in order to create a Host instance, but I have implemented the djangorestframework-api-key library.
So my request from Insomnia (can use Postman too I guess) looks like this:

The Header has the API key in the Authorization which I won't show.
Now, when I send this Post request, the host_developer_email field is NULL, which I expected - but I need it to have something in order to be able to grab the correct row in future when using this data in machines/servers.
Or, is this the wrong way to do things? Should I have two seperate Hosts models - one for users on the website and another for third-party backends and services (i.e. machines), with the latter having no host_developer_email field and the host_label field being unique?