Django SELECT(1)AS [a] FROM [my_table] WHERE([my_table]。[id] =?AND NOT([my_table]。[id] =?))(1,1)

时间:2010-02-11 02:21:20

标签: django orm

为什么Django会执行如下语句:

SELECT (1) AS [a] FROM [my_table] 
WHERE ([my_table].[id] = ?  
AND NOT ([my_table].[id] = ? )) (1, 1)

在以下列方式创建的formset上调用is_valid()时会发生这种情况:

MyFormSet = modelformset_factory(Table, fields=['my_field'], extra=0)
my_form_set = MyFormSet(request.POST,
                        queryset=Table.objects.all())

表和MyForm一样简单,比如说:

class Table(models.Model):
    my_field = models.CharField(max_length=10)

class MyForm(forms.ModelForm):
    class Meta:
        model = Table

提示:我查看了调用堆栈,负责它的代码(在django / forms / models.py中)如下:

def _perform_unique_checks(self, unique_checks):
    import pdb; pdb.set_trace()
    bad_fields = set()
    form_errors = []

    for unique_check in unique_checks:
        # Try to look up an existing object with the same values as this
        # object's values for all the unique field.

        lookup_kwargs = {}
        for field_name in unique_check:
            lookup_value = self.cleaned_data[field_name]
            # ModelChoiceField will return an object instance rather than
            # a raw primary key value, so convert it to a pk value before
            # using it in a lookup.
            if isinstance(self.fields[field_name], ModelChoiceField):
                lookup_value =  lookup_value.pk
            lookup_kwargs[str(field_name)] = lookup_value

        qs = self.instance.__class__._default_manager.filter(**lookup_kwargs)

        # Exclude the current object from the query if we are editing an
        # instance (as opposed to creating a new one)
        if self.instance.pk is not None:
            qs = qs.exclude(pk=self.instance.pk)

基本上,pk都包含在唯一性检查中并被排除在外。看起来Django可以更聪明,避免这种低效率。

2 个答案:

答案 0 :(得分:1)

我没有详细看过它,但我认为你是对的,Django可以快捷查询这个问题。请在http://code.djangoproject.com/提交一张票。

答案 1 :(得分:0)

看起来已经在trunk中修复了这个问题(通过添加新的功能来解决这个特殊问题)