NOT NULL constraint failed: customer_management_lead.organisation_id

MODELS | models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.signals import post_save
from django import forms

class User(AbstractUser):
is_organiser = models.BooleanField(default=True)
is_agent = models.BooleanField(default=False)

class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)

def __str__(self):
    return self.user.username

class Lead(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
age = models.IntegerField(default=0)
organisation = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
agent = models.ForeignKey(“Agent”, null=True, blank=True, on_delete=models.SET_NULL)

def __str__(self):
    return f"{self.first_name} {self.last_name}"

class Agent(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
organisation = models.ForeignKey(UserProfile, on_delete=models.CASCADE)

def __str__(self):
    return self.user.email

def post_user_created_signal(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)

post_save.connect(post_user_created_signal, sender=User)

views.py

from django.core.mail import send_mail
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth import logout
from django.db.models.query import QuerySet
from django.forms import BaseModelForm
from django.http import HttpResponse, request
from django.shortcuts import render, redirect, reverse
from django.contrib import messages
from django.views import generic
from .models import *
from .forms import *
from .mixin import *
import os

CRUD: Create, Retrieve, Update, Delete.

class HomeView(generic.TemplateView):
template_name = “home.html”

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context["title"] = "Home"
    return context

class LeadListView(OrganiserLoginRequiredMixin, generic.ListView):
template_name = “lead_list.html”
context_object_name = “leads”

def get_queryset(self):
    user = self.request.user

    # initial queryset of lead for the entire organisation
    if user.is_organiser:
        queryset = Lead.objects.filter(organisation=user.userprofile)
    else:
        queryset = Lead.objects.filter(organisation=user.agent.organisation)
        # filter for the agent that is logged in
        queryset = queryset.filter(agent__user=user)
    return queryset

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context["title"] = "Lead List"
    return context

class LeadDetailView(OrganiserLoginRequiredMixin, generic.DetailView):
template_name = “lead_detail.html”
context_object_name = “lead”

def get_queryset(self):
    user = self.request.user

    # initial queryset of lead for the entire organisation
    if user.is_organiser:
        queryset = Lead.objects.filter(organisation=user.userprofile)
    else:
        queryset = Lead.objects.filter(organisation=user.agent.organisation)
        # filter for the agent that is logged in
        queryset = queryset.filter(agent__user=user)
    return queryset

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context["title"] = f"Details for {self.object}"
    return context

class LeadCreateView(OrganiserLoginRequiredMixin, generic.CreateView):
template_name = “register_lead.html”
form_class = LeadModelForm

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context["title"] = "Create Lead"
    return context

def form_valid(self, form):
    send_mail(
        subject="New Lead created",
        message=(f"Lead created successfully for {form.cleaned_data['first_name']} {form.cleaned_data['last_name']}"),
        from_email=os.environ.get("WIRE_APP_EMAIL_USER"),
        recipient_list=[os.environ.get("WIRE_APP_EMAIL_USER")],
    )
    return super(LeadCreateView, self).form_valid(form)

def get_success_url(self):
    return reverse("home")

class LeadUpdateView(OrganiserLoginRequiredMixin, generic.UpdateView):
template_name = “lead_update.html”
form_class = LeadModelForm

def get_queryset(self):
    user = self.request.user
    # initial queryset of lead for the entire organisation
    return Lead.objects.filter(organisation=user.userprofile)

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context["title"] = f"Update for {self.object}"
    return context

def get_success_url(self):
    return reverse("lead_list")

class LeadDeleteView(OrganiserLoginRequiredMixin, generic.DeleteView):
template_name = “lead_delete.html”

def get_queryset(self):
    user = self.request.user
    # initial queryset of lead for the entire organisation
    return Lead.objects.filter(organisation=user.userprofile)

def get_success_url(self):
    return reverse("lead_list")

class SignUpView(generic.CreateView):
template_name = “registration/signup.html”
form_class = UserRegistrationForm

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context["title"] = "Sign Up"
    return context

def get_success_url(self):
    return reverse("login")

def logout_view(request):
logout(request)
return redirect(“login”)

class AgentListView(OrganiserLoginRequiredMixin, generic.ListView):
template_name = “agent_list.html”

def get_queryset(self):
    organisation = self.request.user.userprofile
    return Agent.objects.filter(organisation=organisation)

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context["title"] = "Agent List"
    return context

class AgentCreateView(OrganiserLoginRequiredMixin, generic.CreateView):
template_name = “register_agent.html”
form_class = AgentModelForm

def get_success_url(self):
    return reverse("agent_list")

def form_valid(self, form):
    agent = form.save(commit=False)
    agent.organisation = self.request.user.userprofile
    agent.save()
    return super(AgentCreateView, self).form_valid(form)

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context["title"] = "Agent Create"
    return context

class AgentDetailView(OrganiserLoginRequiredMixin, generic.DetailView):
template_name = “agent_detail.html”

def get_queryset(self):
    organisation = self.request.user.userprofile
    return Agent.objects.filter(organisation=organisation)

def get_queryset(self):
    organisation = self.request.user.userprofile
    return Agent.objects.filter(organisation=organisation)

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context["title"] = f"Details for {self.object}"
    return context

class AgentUpdateView(OrganiserLoginRequiredMixin, generic.UpdateView):
template_name = “agent_update.html”
form_class = AgentModelForm

def get_queryset(self):
    organsation = self.request.user.username
    return Agent.objects.filter(organsation=organsation)

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context["title"] = f"Update for {self.object}"
    return context

def get_success_url(self):
    return reverse("agent_detail", kwargs={"pk": self.object.pk})

class AgentDeleteView(OrganiserLoginRequiredMixin, generic.DeleteView):
template_name = “agent_delete.html”

def get_queryset(self):
    organisation = self.request.user.userprofile
    return Agent.objects.filter(organisation=organisation)

def get_success_url(self):
    return reverse("agent_list")

Please format your code in code blocks and describe your problem clearly.

change organisation option(null = True)

class Lead(models.Model):
  # organisation = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
  organisation = models.ForeignKey(UserProfile, on_delete=models.CASCADE, null=True)
  ...

OR change form_vaild method in CU.

def form_valid(self, form):
  obj = form.save(commit=False)
  obj.oranisation = {some value}
  ...
  return super().form_valid(obj)

ok thanks for your help it solved
but in
organisation in models.py null and blank both will be false

model change, should need run migration.