Django KeyError when getting object with keyword for field name

I want to get the object as follows:

Collection.objects.get(name='name', type='library', owner=owner, parent=parent)

Unfortunately type, this is a keyword, so the following error is generated:

KeyError at /forms/create_library
type

Is there a way to eliminate the meaning of the word typeso that I can indicate the field of this name?

+3
source share
2 answers

Not tested:

Collection.objects.filter(
    name__exact='name', 
    type__exact='library', 
    owner__exact=owner, 
    parent__exact=parent)

Request Docs: http://docs.djangoproject.com/en/dev/topics/db/queries/

Also consider naming your field differently, mainly not with the same name as the built-in.

+3
source

, , . , , self.cleaned_data .

self.cleaned_data['type'], "library". , , KeyError.

0

Source: https://habr.com/ru/post/1726235/


All Articles