Django

Code

Changeset 7511

Show
Ignore:
Timestamp:
04/30/08 19:32:50 (2 months ago)
Author:
jbronn
Message:

gis: Merged revisions 7499,7501-7502,7504,7509-7510 via svnmerge from trunk.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis

    • Property svnmerge-integrated changed from /django/trunk:1-7497 to /django/trunk:1-7510
  • django/branches/gis/django/db/models/base.py

    r7482 r7511  
    238238        dispatcher.send(signal=signals.post_init, sender=self.__class__, instance=self) 
    239239 
    240     def from_sequence(cls, values): 
    241         """ 
    242         An alternate class constructor, primarily for internal use. 
    243  
    244         Creates a model instance from a sequence of values (which corresponds 
    245         to all the non-many-to-many fields in creation order. If there are more 
    246         fields than values, the remaining (final) fields are given their 
    247         default values. 
    248  
    249         ForeignKey fields can only be initialised using id values, not 
    250         instances, in this method. 
    251         """ 
    252         dispatcher.send(signal=signals.pre_init, sender=cls, args=values, 
    253                 kwargs={}) 
    254         obj = Empty() 
    255         obj.__class__ = cls 
    256         field_iter = iter(obj._meta.fields) 
    257         for val, field in izip(values, field_iter): 
    258             setattr(obj, field.attname, val) 
    259         for field in field_iter: 
    260             setattr(obj, field.attname, field.get_default()) 
    261         dispatcher.send(signal=signals.post_init, sender=cls, instance=obj) 
    262         return obj 
    263  
    264     from_sequence = classmethod(from_sequence) 
    265  
    266240    def __repr__(self): 
    267241        return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self))) 
  • django/branches/gis/django/db/models/query.py

    r7498 r7511  
    2929    ######################## 
    3030 
     31    def __getstate__(self): 
     32        """ 
     33        Allows the Queryset to be pickled. 
     34        """ 
     35        # Force the cache to be fully populated. 
     36        len(self) 
     37 
     38        obj_dict = self.__dict__.copy() 
     39        obj_dict['_iter'] = None 
     40        return obj_dict 
     41 
    3142    def __repr__(self): 
    3243        return repr(list(self)) 
     
    3849        if self._result_cache is None: 
    3950            if self._iter: 
    40                 self._result_cache = list(self._iter()
     51                self._result_cache = list(self._iter
    4152            else: 
    4253                self._result_cache = list(self.iterator()) 
     
    154165                        max_depth, requested=requested) 
    155166            else: 
    156                 obj = self.model.from_sequence(row[index_start:]) 
     167                obj = self.model(*row[index_start:]) 
    157168            for i, k in enumerate(extra_select): 
    158169                setattr(obj, k, row[i]) 
     
    645656    restricted = requested is not None 
    646657    index_end = index_start + len(klass._meta.fields) 
    647     obj = klass.from_sequence(row[index_start:index_end]) 
     658    obj = klass(*row[index_start:index_end]) 
    648659    for f in klass._meta.fields: 
    649660        if (not f.rel or (not restricted and f.null) or 
  • django/branches/gis/django/db/models/sql/query.py

    r7498 r7511  
    9999        memo[id(self)] = result 
    100100        return result 
     101 
     102    def __getstate__(self): 
     103        """ 
     104        Pickling support. 
     105        """ 
     106        obj_dict = self.__dict__.copy() 
     107        del obj_dict['connection'] 
     108        return obj_dict 
     109 
     110    def __setstate__(self, obj_dict): 
     111        """ 
     112        Unpickling support. 
     113        """ 
     114        self.__dict__.update(obj_dict) 
     115        # XXX: Need a better solution for this when multi-db stuff is 
     116        # supported. It's the only class-reference to the module-level 
     117        # connection variable. 
     118        self.connection = connection 
    101119 
    102120    def get_meta(self): 
     
    377395        """ 
    378396        qn = self.quote_name_unless_alias 
    379         result = ['(%s) AS %s' % (col, alias) for alias, col in self.extra_select.iteritems()] 
     397        qn2 = self.connection.ops.quote_name 
     398        result = ['(%s) AS %s' % (col, qn2(alias)) for alias, col in self.extra_select.iteritems()] 
    380399        aliases = set(self.extra_select.keys()) 
    381400        if with_aliases: 
  • django/branches/gis/django/db/models/sql/where.py

    r7482 r7511  
    5252                elif isinstance(child, tree.Node): 
    5353                    sql, params = self.as_sql(child, qn) 
    54                     if len(child.children) == 1: 
     54                    if child.negated: 
     55                        format = 'NOT (%s)' 
     56                    elif len(child.children) == 1: 
    5557                        format = '%s' 
    5658                    else: 
    5759                        format = '(%s)' 
    58                     if child.negated: 
    59                         format = 'NOT %s' % format 
    6060                else: 
    6161                    sql, params = self.make_atom(child, qn) 
  • django/branches/gis/docs/db-api.txt

    r7498 r7511  
    376376      iterating over a ``QuerySet`` will take advantage of your database to 
    377377      load data and instantiate objects only as you need them. 
     378 
     379 
     380Pickling QuerySets 
     381~~~~~~~~~~~~~~~~~~ 
     382 
     383If you pickle_ a ``QuerySet``, this will also force all the results to be 
     384loaded into memory prior to pickling. This is because pickling is usually used 
     385as a precursor to caching and when the cached queryset is reloaded, you want 
     386the results to already be present. This means that when you unpickle a 
     387``QuerySet``, it contains the results at the moment it was pickled, rather 
     388than the results that are currently in the database. 
     389 
     390If you only want to pickle the necessary information to recreate the 
     391``Queryset`` from the database at a later time, pickle the ``query`` attribute 
     392of the ``QuerySet``. You can then recreate the original ``QuerySet`` (without 
     393any results loaded) using some code like this:: 
     394 
     395    >>> import pickle 
     396    >>> query = pickle.loads(s)     # Assuming 's' is the pickled string. 
     397    >>> qs = MyModel.objects.all() 
     398    >>> qs.query = query            # Restore the original 'query'. 
     399 
     400.. _pickle: http://docs.python.org/lib/module-pickle.html 
    378401 
    379402Limiting QuerySets 
  • django/branches/gis/docs/request_response.txt

    r7405 r7511  
    403403      object. Doing so will raise ``Exception``. 
    404404 
     405Setting headers 
     406~~~~~~~~~~~~~~~ 
     407 
     408To set a header in your response, just treat it like a dictionary:: 
     409 
     410    >>> response = HttpResponse() 
     411    >>> response['Pragma'] = 'no-cache' 
     412 
     413Telling the browser to treat the response as a file attachment 
     414~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     415 
     416To tell the browser to treat the response as a file attachment, use the 
     417``mimetype`` argument and set the ``Content-Disposition`` header. For example, 
     418this is how you might return a Microsoft Excel spreadsheet:: 
     419 
     420    >>> response = HttpResponse(my_data, mimetype='application/vnd.ms-excel') 
     421    >>> response['Content-Disposition'] = 'attachment; filename=foo.xls' 
     422 
     423There's nothing Django-specific about the ``Content-Disposition`` header, but 
     424it's easy to forget the syntax, so we've included it here. 
     425 
    405426Methods 
    406427------- 
     
    421442    header, it can also include the character set encoding, which makes it 
    422443    more than just a MIME type specification. If ``mimetype`` is specified 
    423     (not None), that value is used. Otherwise, ``content_type`` is used. If 
     444    (not ``None``), that value is used. Otherwise, ``content_type`` is used. If 
    424445    neither is given, the ``DEFAULT_CONTENT_TYPE`` setting is used. 
    425446 
  • django/branches/gis/tests/modeltests/basic/models.py

    r7482 r7511  
    399399>>> Article.objects.get(headline='Article 11') in s 
    400400True 
     401 
     402# The 'select' argument to extra() supports names with dashes in them, as long 
     403# as you use values(). 
     404>>> Article.objects.filter(pub_date__year=2008).extra(select={'dashed-value': '1'}).values('headline', 'dashed-value') 
     405[{'headline': u'Article 11', 'dashed-value': 1}, {'headline': u'Article 12', 'dashed-value': 1}] 
     406 
     407# If you use 'select' with extra() and names containing dashes on a query 
     408# that's *not* a values() query, those extra 'select' values will silently be 
     409# ignored. 
     410>>> articles = Article.objects.filter(pub_date__year=2008).extra(select={'dashed-value': '1', 'undashedvalue': '2'}) 
     411>>> articles[0].undashedvalue 
     4122 
    401413""" 
  • django/branches/gis/tests/regressiontests/queries/models.py

    r7498 r7511  
    121121class CustomManager(models.Manager): 
    122122    def get_query_set(self): 
    123         return super(CustomManager, self).get_query_set().filter(public=True, 
    124                 tag__name='t1') 
     123        qs = super(CustomManager, self).get_query_set() 
     124        return qs.filter(is_public=True, tag__name='t1') 
    125125 
    126126class ManagedModel(models.Model): 
    127127    data = models.CharField(max_length=10) 
    128128    tag = models.ForeignKey(Tag) 
    129     public = models.BooleanField(default=True) 
     129    is_public = models.BooleanField(default=True) 
    130130 
    131131    objects = CustomManager() 
     
    699699Updates that are filtered on the model being updated are somewhat tricky to get 
    700700in MySQL. This exercises that case. 
    701 >>> mm = ManagedModel.objects.create(data='mm1', tag=t1, public=True) 
     701>>> mm = ManagedModel.objects.create(data='mm1', tag=t1, is_public=True) 
    702702>>> ManagedModel.objects.update(data='mm') 
    703703