0

I am building a CRM where I want each client to have multiple plans, and each plan to have multiple notes. When a user creates a new note, I want them to be able to select a relevant plan from a dropdown of plans belonging to the client. From what I can find, I should be able to get the contact_id from the kwargs, but my errors show nothing in kwargs. I know there should be a way to do this, but I can't seem to find it.

Variable    Value
__class__   <class 'lynx.forms.SipNoteForm'>
args    ()
kwargs  {}
self    <SipNoteForm bound=False, valid=Unknown, fields=(sip_plan;note;note_date;fiscal_year;quarter;class_hours;instructor;clients)>

Views.py

@login_required
def add_sip_note(request, contact_id):
    form = SipNoteForm()
    if request.method == 'POST':
        form = SipNoteForm(request.POST)
        if form.is_valid():
            form = form.save(commit=False)
            form.contact_id = contact_id
            form.user_id = request.user.id
            form.save()
            return HttpResponseRedirect(reverse('lynx:client', args=(contact_id,)))
    return render(request, 'lynx/add_sip_note.html', {'form': form})

Forms.py

class SipNoteForm(forms.ModelForm):
    class Meta:
        model = SipNote
        exclude = ('created', 'modified', 'user', 'contact')

    def __init__(self, *args, **kwargs):
        super(SipNoteForm, self).__init__(*args, **kwargs)
        self.fields['sip_plan'].queryset = SipPlan.objects.filter(contact_id=kwargs.get("contact_id"))

Urls.py

path('add-sip-note/<int:contact_id>/', views.add_sip_note, name='add_sip_note'),

1 Answer 1

1

You are trying to get the kwargs in __init__(self, *args, **kwargs) as

    def __init__(self, *args, **kwargs):
        contact_id = kwargs.pop('contact_id')
        super(SipNoteForm, self).__init__(*args, **kwargs)
        self.fields['sip_plan'].queryset = SipPlan.objects.filter(contact_id=contact_id)

But you are not passing contact_id kwargs to the form while posting. you should pass kwargs to the form you are going to get in __init__(self, *args, **kwargs) such as

@login_required
def add_sip_note(request, contact_id):
    form = SipNoteForm()
    if request.method == 'POST':
        form = SipNoteForm(request.POST, contact_id=contact_id)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I updated my code to include form = SipNoteForm(request.POST, contact_id=contact_id), but I think I also need it when I definte form earlier, in form = SipNoteForm(). I tried form = SipNoteForm(contact_id=contact_id), form = SipNoteForm(None, contact_id=contact_id) and form = SipNoteForm(request, contact_id=contact_id) but all of those gave me the same error __init__() got an unexpected keyword argument 'contact_id'
Oh, I figured it out, I wrote my view initiation like contact = {'contact_id': contact_id} form = SipNoteForm(**contact) And in my forms, I popped contact_id out of the kwargs before the __init__, so def __init__(self, *args, **kwargs): contact_id = kwargs.pop('contact_id') super(SipNoteForm, self).__init__(*args, **kwargs) self.fields['sip_plan'].queryset = SipPlan.objects.filter(contact_id=contact_id) Thank you for your help, I was really stuck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.