'How to create record when click on button

I'm trying to convert lead to an account in a crm app.

From my lead detail template, if i click "Convert" button, it need to add new row in account table with few information from the leads.

I tried to pass value using id to view so I can filter the lead object using the ID and then add those values in account table.

kindly note that, i have written views code with my limitted knowledge on Django. It maybe completely wrong. I have tried many options and read many post, but i couldnt figure it out.

model:

class Lead(models.Model):
  

    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    annual_revenue = models.IntegerField(null=True, blank=True)
    company_name = models.CharField(max_length=30, null=True, blank=True)
    website = models.CharField(max_length=30, null=True, blank=True)
    city = models.CharField(max_length=30, null=True, blank=True)
    country = models.CharField(max_length=30, null=True, blank=True)
    description = models.TextField(max_length=255, null=True, blank=True)
    

class Account(models.Model):

    account_name = models.CharField(max_length=30, null=True, blank=True) # company name from Lead
    annual_revenue = models.IntegerField(null=True, blank=True)
    phone = models.CharField(max_length=30, null=True, blank=True)
    website = models.CharField(max_length=30, null=True, blank=True)

Views:

class LeadConvert(generic.CreateView):
    def get_success_url(self):
        return "/leads"

    def ConvertLead(self, **kwargs):
        lead = self.queryset
        account = Account.objects.create(
            account_name = lead.company_name,
            annual_revenue = lead.annual_revenue,
            website = lead.website
        )
        account.save

    def get_queryset(self, **kwargs):
        queryset = Lead.objects.filter[id == id]
        return queryset

urls

path('lead/converted/<int:pk>', LeadConvert.as_view(), name = 'lead-convert')

html

<a class="btn btn-primary" href="{% url 'leads:lead-convert' leads.pk%}" role="button">Convert</a>

form

class LeadModelForm(forms.ModelForm):
    class Meta:
        model = Lead
        fields = (
            'first_name',
            'last_name',
            'annual_revenue',
            'company_name',
            'website',
            'city',
            'country',
            )

class AccountModelForm(forms.ModelForm):
    class Meta:
        model = Account
        fields = (
            'account_owner',
            'account_name',
            'annual_revenue',
            'website',
        )


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source