﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22728	get_or_create with field lookups cause empty values	Patrick Bregman	Andriy Sokolovskiy	"First of, I'm actually using 1.7b4, but there seems to be no version for that (yet)...

When trying to be smart and using a case insensitive filter for get_or_create, it doesn't actually fill in the value, because you passed it in with a field lookup instead of just the field name. An example explains this better:


{{{
from django.db import models

class Tag(models.Model):
    name = models.CharField(unique=True)
    slug = models.SlugField(unique=True)

tags = ['django', 'python', 'web', 'Django', 'html5', 'Python'] # Just a list of tags to make this work

for tag in tags:
    obj, created = Tag.objects.get_or_create(name__iexact=tag, defaults={'slug': tag)
    print(""Tag {0} created? {1}"".format(obj.name, created)
}}}

This will give a IntegrityError while going through the list, complaining that there already is a tag with an empty value for name.

{{{
IntegrityError: duplicate key value violates unique constraint ""blog_tag_name_key""
DETAIL:  Key (name)=() already exists.
}}}

A work-around is to also specify the name field in the defaults, but I was actually expecting the ORM to be ""smart"" enough to not require this. There is no [https://docs.djangoproject.com/en/1.7/ref/models/querysets/#get-or-create documentation] describing it should happen either way. Is this a documentation issue or a code issue?"	Bug	closed	Database layer (models, ORM)	dev	Normal	invalid	get_or_create		Accepted	1	0	0	0	0	0
