Opened 3 years ago

Closed 3 years ago

#33478 closed Cleanup/optimization (invalid)

Can't use QuerySet.get_or_create() with a field named defaults

Reported by: MojixCoder Owned by: nobody
Component: Database layer (models, ORM) Version: 4.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

currently in Django we have get_or_create(defaults=None, **kwargs), which works fine.
But what if our model has a field called defaults.
Then how can we query by that field.
Maybe we should change defaults to _defaults?

Change History (1)

comment:1 by Tim Graham, 3 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: invalid
Status: newclosed
Summary: Change get_or_create input parametersCan't use QuerySet.get_or_create() with a field named defaults

From the get_or_create() docs:

If you have a field named defaults and want to use it as an exact lookup in get_or_create(), just use 'defaults__exact', like so:

Foo.objects.get_or_create(defaults__exact='bar', defaults={'defaults': 'baz'})
Note: See TracTickets for help on using tickets.
Back to Top