~ubuntu-branches/ubuntu/saucy/python-django/saucy-updates

« back to all changes in this revision

Viewing changes to django/contrib/contenttypes/generic.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone, Jakub Wilk, Luke Faraone
  • Date: 2013-05-09 15:10:47 UTC
  • mfrom: (1.1.21) (4.4.27 sid)
  • Revision ID: package-import@ubuntu.com-20130509151047-aqv8d71oj9wvcv8c
Tags: 1.5.1-2
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Luke Faraone ]
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""
2
2
Classes allowing "generic" relations through ContentType and object-id fields.
3
3
"""
 
4
from __future__ import unicode_literals
4
5
 
5
6
from collections import defaultdict
6
7
from functools import partial
7
 
from operator import attrgetter
8
8
 
9
9
from django.core.exceptions import ObjectDoesNotExist
10
10
from django.db import connection
16
16
from django.forms.models import BaseModelFormSet, modelformset_factory, save_instance
17
17
from django.contrib.admin.options import InlineModelAdmin, flatten_fieldsets
18
18
from django.contrib.contenttypes.models import ContentType
19
 
from django.utils.encoding import smart_unicode
 
19
from django.utils.encoding import smart_text
20
20
 
21
21
class GenericForeignKey(object):
22
22
    """
131
131
 
132
132
    def __set__(self, instance, value):
133
133
        if instance is None:
134
 
            raise AttributeError(u"%s must be accessed via instance" % self.related.opts.object_name)
 
134
            raise AttributeError("%s must be accessed via instance" % self.related.opts.object_name)
135
135
 
136
136
        ct = None
137
137
        fk = None
168
168
 
169
169
    def value_to_string(self, obj):
170
170
        qs = getattr(obj, self.name).all()
171
 
        return smart_unicode([instance._get_pk_val() for instance in qs])
 
171
        return smart_text([instance._get_pk_val() for instance in qs])
172
172
 
173
173
    def m2m_db_table(self):
174
174
        return self.rel.to._meta.db_table
328
328
                    set(obj._get_pk_val() for obj in instances)
329
329
                }
330
330
            qs = super(GenericRelatedObjectManager, self).get_query_set().using(db).filter(**query)
 
331
            # We (possibly) need to convert object IDs to the type of the
 
332
            # instances' PK in order to match up instances:
 
333
            object_id_converter = instances[0]._meta.pk.to_python
331
334
            return (qs,
332
 
                    attrgetter(self.object_id_field_name),
 
335
                    lambda relobj: object_id_converter(getattr(relobj, self.object_id_field_name)),
333
336
                    lambda obj: obj._get_pk_val(),
334
337
                    False,
335
338
                    self.prefetch_cache_name)
427
430
                                  max_num=None,
428
431
                                  formfield_callback=None):
429
432
    """
430
 
    Returns an ``GenericInlineFormSet`` for the given kwargs.
 
433
    Returns a ``GenericInlineFormSet`` for the given kwargs.
431
434
 
432
435
    You must provide ``ct_field`` and ``object_id`` if they different from the
433
436
    defaults ``content_type`` and ``object_id`` respectively.