# How to get parent ID for queryset filter in form load of CreateView

**URL:** https://forum.djangoproject.com/t/how-to-get-parent-id-for-queryset-filter-in-form-load-of-createview/16758
**Category:** Forms & APIs
**Created:** [November 1, 2022, 7:47pm UTC](https://forum.djangoproject.com/t/how-to-get-parent-id-for-queryset-filter-in-form-load-of-createview/16758 "2022-11-01T19:47:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![yellowlabrat](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/yellowlabrat/32/6201_2.png) [@yellowlabrat](https://forum.djangoproject.com/u/yellowlabrat)
#### Post date: [November 1, 2022, 7:47pm UTC](https://forum.djangoproject.com/t/how-to-get-parent-id-for-queryset-filter-in-form-load-of-createview/16758/1 "2022-11-01T19:47:23Z")

</div>

Hello,

I’m using Class Based Views and trying to get a parent ID in the initial load of the form of a CreateView. I’ve looked through the documentation suggested in other posts ([https://ccbv.co.uk/](https://ccbv.co.uk/)) and ([Django Class Based Views Diagrams](https://www.brennantymrak.com/articles/django-class-based-views-diagrams.html)), but am still unsure of how to accomplish this.

It doesn’t matter if I even need to just use part of the URL to get the parent ID I’m looking for, I’m looking for any way to make this work.

I have a parent object, Client, which has an ID in the url, when creating a child, Note, object. Like this: [http://127.0.0.1:8000/clients/1/note/add](http://127.0.0.1:8000/clients/1/note/add)  
In that case, the “1” is the Client ID that I want to get when the “Add Note” form is loading.

Here is the **urls.py** part of this:  
`path('<int:client_id>/note/add', views.NoteAdd.as_view(), name="note_add"),`

Here is part of the forms.py code that is creating the multi-select field for the Note object. Essentially, I would like to filter the query set for the ‘goals’ field to only include Objective records for the parent Client ID of this object.

**forms.py**

```auto
class NoteForm(ModelForm):
	class Meta:
		model = Note
		exclude = ['client']
		widgets = {
			'service_date': widgets.DateInput(attrs={'type': 'date'}),
			'start_time': widgets.TimeInput(attrs={'type': 'time'}),
			'end_time': widgets.TimeInput(attrs={'type': 'time'}),
		}

	def __init__ (self, *args, **kwargs):
		super(). __init__ (*args, **kwargs)
		self.helper = FormHelper()

		# Create multi-select checkboxes that only have the values for this specific client
		self.fields['goals'] = ModelMultipleChoiceField(
				#queryset=Objective.objects.filter(client=kwargs.get('instance').client.id),
				queryset=Objective.objects.filter(client=self.kwargs['client_id']),
				#queryset=Objective.objects.filter(client=client_id),
				widget=CheckboxSelectMultiple
			)

```

You can see the multiple (commented out) ways I’ve tried to access that “client\_id” on the NoteForm when it first loads. This obviously works fine when editing a previously saved NoteForm, but not on the initial creation of a new Note.

Here is the Note object in **models.py**

```auto
class Note(TheBaseClass):
    client = models.ForeignKey(Client, on_delete=models.CASCADE)
    service_date = models.DateField()
    start_time = models.TimeField(blank=True, null=True)
    end_time = models.TimeField(blank=True, null=True)
    goals = models.ManyToManyField("Objective", verbose_name="Goals addressed")

```

I’m not sure if this helps, but here is part of the **views.py** file as well.

```auto
class NoteAdd(UserAccessMixin, SuccessMessageMixin, generic.CreateView):
    permission_required = 'clients.add_note'

    model = Note
    form_class = NoteForm
    template_name = 'generic_add.html'
    success_message = model._meta.verbose_name + " added successfully."

```

The ultimate error message is:

```auto
# AttributeError at /clients/1/note/add
'NoteForm' object has no attribute 'kwargs'

```

This makes sense, because I suppose kwargs don’t exist yet at the time the form is being rendered, but I’m looking for a way to get that “client\_id” from the URL and use that in the queryset filter.

Knowing all of this, how do I get the “client\_id” (present in the url) to be used on NoteAdd load?

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [November 1, 2022, 7:59pm UTC](https://forum.djangoproject.com/t/how-to-get-parent-id-for-queryset-filter-in-form-load-of-createview/16758/2 "2022-11-01T19:59:59Z")

</div>

All named url parameters are made available in a CBV through the `self.kwargs` object. See the docs and examples at [Built-in class-based generic views | Django documentation | Django](https://docs.djangoproject.com/en/4.1/topics/class-based-views/generic-display/#dynamic-filtering)

This would be accessed in one of the CBV functions being called - it cannot be used at the module level.  
So you might take advantage of this in one of `get_queryset`, `get_object`, `get_context_data`, etc - wherever you need to reference it in the process of constructing your response.

This is where using tools like the CCBV site can be really helpful for understanding the flow of a CBV and learning where you might want to hook in some custom functionality.

---

<div class="post-metadata">

### Author: ![yellowlabrat](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/yellowlabrat/32/6201_2.png) [@yellowlabrat](https://forum.djangoproject.com/u/yellowlabrat)
#### Post date: [November 3, 2022, 12:13pm UTC](https://forum.djangoproject.com/t/how-to-get-parent-id-for-queryset-filter-in-form-load-of-createview/16758/3 "2022-11-03T12:13:33Z")

</div>

Thank you for the response.  
After reviewing the documentation, I still don’t see something that covers the specifics of what I’m looking for.

As I understand it, the `get_queryset` function is being used to filter the **current** object. In the example I gave, that would be the Note object.

But on that Note object’s form in the “goals” field, I want to filter the queryset for the **parent** object.

As in, the Note object’s “goals” field, needs to be filtered to only include the parent “Client” object’s (in this case ID: 1) goals.

---

<div class="post-metadata">

### Author: ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)
#### Post date: [November 3, 2022, 12:29pm UTC](https://forum.djangoproject.com/t/how-to-get-parent-id-for-queryset-filter-in-form-load-of-createview/16758/4 "2022-11-03T12:29:49Z")

</div>

From my understanding you want to access the view’s `kwargs` from within the form. While you can’t do that out of the box, you could pass the value specifically to the form. This can be done by adding it to the return value of [` get_form_kwargs`](https://docs.djangoproject.com/en/4.1/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin.get_form_kwargs) in your view. Like Ken mentioned, it’s easier to reason about the view’s functions using the [Classy CBV site](https://ccbv.co.uk/projects/Django/4.1/django.views.generic.edit/CreateView/).

---

<div class="post-metadata">

### Author: ![yellowlabrat](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/yellowlabrat/32/6201_2.png) [@yellowlabrat](https://forum.djangoproject.com/u/yellowlabrat)
#### Post date: [November 4, 2022, 10:56am UTC](https://forum.djangoproject.com/t/how-to-get-parent-id-for-queryset-filter-in-form-load-of-createview/16758/5 "2022-11-04T10:56:42Z")

</div>

@CodenameTim Thank you so much for pointing me in the right direction of a function I could try!  
This is working!

updated **views.py** class NoteAdd

```auto
def get_form_kwargs(self):
	"""Return the keyword arguments for instantiating the form."""
	kwargs = super().get_form_kwargs()
	# Adding in client_id to the kwargs on load, so the forms.py NoteForm can access it.
	client_id_from_url = self.kwargs['client_id']

	if hasattr(self, "object"):
		# Normal syntax
		#kwargs.update({"instance": self.object})

		# Adding in client_id_from_url as a new parameter for the __init__ in forms.py NoteForm
		kwargs.update({"instance": self.object,
					   "client_id_from_url": client_id_from_url})
	return kwargs

```

Then, over in **forms.py** class NoteForm  
`def __init__ (self, client_id_from_url, *args, **kwargs):`

allowed me to use the new client\_id\_from\_url variable:

```auto
# Create multi-select checkboxes that only have the values for this specific client
self.fields['goals'] = ModelMultipleChoiceField(
		queryset=Objective.objects.filter(client=client_id_from_url),
		widget=CheckboxSelectMultiple
)

```

Now, simply out of wanting to understand better, why am I able to access `self.kwargs['client_id']` inside of the views.py NoteAdd class, but not in the forms.py NoteForm ` __init__ ` function?

I realize “self” is different in both cases, but it would seem if the “client\_id” kwarg is available to the NoteAdd’s `get_form_kwargs` function, and the NoteForm’s ` __init__ ` function is using the `get_form_kwargs` function, then shouldn’t the client\_id kwarg already exist?

Or is it that the NoteForm’s ` __init__ ` function is the first thing being called, which then fires the get\_form\_kwargs function?

---

<div class="post-metadata">

### Author: ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)
#### Post date: [November 4, 2022, 1:33pm UTC](https://forum.djangoproject.com/t/how-to-get-parent-id-for-queryset-filter-in-form-load-of-createview/16758/6 "2022-11-04T13:33:31Z")

</div>

[`self.kwargs` for the view is set from the `View.setup`](https://ccbv.co.uk/projects/Django/4.1/django.views.generic.edit/CreateView/#setup). It’s specific to Views. Forms don’t have that same setup. Forms are somewhat removed from the concept of a request, response and view. Does that help clear up the discrepancy? If not, please let me know where I should explain more.

---

<div class="post-metadata">

### Author: ![yellowlabrat](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/yellowlabrat/32/6201_2.png) [@yellowlabrat](https://forum.djangoproject.com/u/yellowlabrat)
#### Post date: [November 4, 2022, 2:47pm UTC](https://forum.djangoproject.com/t/how-to-get-parent-id-for-queryset-filter-in-form-load-of-createview/16758/7 "2022-11-04T14:47:09Z")

</div>

> [@CodenameTim](#):
>
> Forms are somewhat removed from the concept of a request, response and view.

I think this helps explain it some. And the rest will come from time/experience, probably. 🙂

Thank you, again, for your help!

---

<div class="post-metadata">

### Author: ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)
#### Post date: [November 4, 2022, 2:58pm UTC](https://forum.djangoproject.com/t/how-to-get-parent-id-for-queryset-filter-in-form-load-of-createview/16758/8 "2022-11-04T14:58:24Z")

</div>

You can use the python debugger or your IDE’s debugger to step through a view and form’s process. That can help you understand how it all works together.

Most of my knowledge came from diving into the code and/or repeatedly reading the docs trying to solve my problems and others’.
