Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django KeyError when getting an object with a keyword for a field name

Tags:

django

I wish to get an object in the following fashion:

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

Unfortunately type is a keyword as thus creates the following error:

KeyError at /forms/create_library
type

Is there a way to disambiguate the meaning of the word type to allow me to specify a field of that name?

like image 287
Marcus Whybrow Avatar asked Dec 10 '25 22:12

Marcus Whybrow


1 Answers

Not tested:

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

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

Also consider naming your field differently, mainly not with the same name as a builtin.

like image 95
miku Avatar answered Dec 14 '25 09:12

miku



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!