You can use the pattern described in the documentation for adding request.user - it's the same principle. Remove env
from the fields list, then define form_valid()
:
class EnvhostsCreate(CreateView):
model = Capacity.models.Envhosts
fields = ['hostname', 'count']
template_name_suffix = '_create_form'
def form_valid(self, form):
form.instance.env = Envhosts.objects.get(pk=self.kwargs['envid'])
return super(EnvhostsCreate, self).form_valid(form)