Ticket #8990: reset-extra.diff

File reset-extra.diff, 7.0 KB (added by miracle2k, 16 years ago)
  • django/db/models/base.py

    diff --git a/django/db/models/base.py b/django/db/models/base.py
    index f94d25c..2653244 100644
    a b class Model(object):  
    351351        if pk_set:
    352352            # Determine whether a record with the primary key already exists.
    353353            if (force_update or (not force_insert and
    354                     manager.filter(pk=pk_val).extra(select={'a': 1}).values('a').order_by())):
     354                    manager.filter(pk=pk_val).\
     355                                                extra(select=False, where=False, params=False, tables=False, order_by=False).\
     356                                                extra(select={'a': 1}).values('a').order_by())):
    355357                # It does already exist, so do an UPDATE.
    356358                if force_update or non_pks:
    357359                    values = [(f, None, f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks]
  • django/db/models/sql/query.py

    diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
    index b4525f1..4d32924 100644
    a b class Query(object):  
    16491649                select_pairs[name] = (entry, entry_params)
    16501650            # This is order preserving, since self.extra_select is a SortedDict.
    16511651            self.extra_select.update(select_pairs)
     1652        elif select is not None:
     1653            self.extra_select = SortedDict()
     1654
    16521655        if where:
    16531656            self.extra_where += tuple(where)
     1657        elif where is not None:
     1658            self.extra_where = ()
     1659
    16541660        if params:
    16551661            self.extra_params += tuple(params)
     1662        elif params is not None:
     1663            self.extra_where = ()
     1664           
    16561665        if tables:
    16571666            self.extra_tables += tuple(tables)
     1667        elif tables is not None:
     1668            self.extra_tables = ()
     1669
    16581670        if order_by:
    16591671            self.extra_order_by = order_by
     1672        elif order_by is not None:
     1673            self.extra_order_by = ()
    16601674
    16611675    def trim_extra_select(self, names):
    16621676        """
  • new file tests/modeltests/extra_save/models.py

    diff --git a/tests/modeltests/extra_save/__init__.py b/tests/modeltests/extra_save/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/modeltests/extra_save/models.py b/tests/modeltests/extra_save/models.py
    new file mode 100644
    index 0000000..cb7ca10
    - +  
     1"""test for bug #8990
     2"""
     3
     4from django.db import models
     5
     6class Shelf(models.Model):
     7    name = models.CharField(max_length=50)
     8
     9    def __unicode__(self):
     10        return u"%s the shelf" % self.name
     11
     12
     13class BookManager(models.Manager):
     14    def get_query_set(self):
     15        return super(BookManager, self).get_query_set().\
     16            select_related('shelf').\
     17            extra(select={'shelf_name': 'extra_save_shelf.name'})
     18
     19
     20class Book(models.Model):
     21    name = models.CharField(max_length=50)
     22    shelf = models.ForeignKey(Shelf)
     23
     24    objects = BookManager()
     25
     26    def __unicode__(self):
     27        return u"%s the book" % self.name
     28
     29
     30__test__ = {'API_TESTS':"""
     31>>> shelf = Shelf(name="Foo")
     32>>> shelf.save()
     33>>> book = Book(name="bar", shelf=shelf)
     34>>> book.save()              # initial save works fine
     35
     36>>> Book.objects.get(pk=1)   # querying works as well
     37<Book: bar the book>
     38
     39# subsequent saves work too...
     40>>> book.save()
     41
     42# ...instead of failing with:
     43#Traceback (most recent call last):
     44#    ...
     45#OperationalError: no such column: extra_save_shelf.name
     46"""
     47}
     48 No newline at end of file
Back to Top