Beispiel #1
0
    def start_object(self, obj):
        """
        Called as each object is handled.
        """
        if not hasattr(obj, "_meta"):
            raise base.SerializationError(
                "Non-model object (%s) encountered during serialization" %
                type(obj))

        self.indent(1)
        attrs = OrderedDict([("model", smart_text(obj._meta))])
        if not self.use_natural_primary_keys or not hasattr(
                obj, 'natural_key'):
            obj_pk = obj._get_pk_val()
            if obj_pk is not None:
                attrs['pk'] = smart_text(obj_pk)

        self.xml.startElement("object", attrs)
Beispiel #2
0
 def handle_m2m(value):
     natural = value.natural_key()
     # Iterable natural keys are rolled out as subelements
     self.xml.startElement("object", {})
     for key_value in natural:
         self.xml.startElement("natural", {})
         self.xml.characters(smart_text(key_value))
         self.xml.endElement("natural")
     self.xml.endElement("object")
Beispiel #3
0
    def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
        """
        Return choices with a default blank choices included, for use as
        SelectField choices for this field.

        Analog of django.db.models.fields.Field.get_choices(), provided
        initially for utilization by RelatedFieldListFilter.
        """
        return (blank_choice if include_blank else []) + [
            (x._get_pk_val(), smart_text(x))
            for x in self.related_model._default_manager.all()
        ]
Beispiel #4
0
 def _start_relational_field(self, field):
     """
     Helper to output the <field> element for relational fields
     """
     self.indent(2)
     self.xml.startElement(
         "field",
         OrderedDict([
             ("name", field.name),
             ("rel", field.remote_field.__class__.__name__),
             ("to", smart_text(field.remote_field.model._meta)),
         ]))
Beispiel #5
0
 def handle_fk_field(self, obj, field):
     """
     Called to handle a ForeignKey (we need to treat them slightly
     differently from regular fields).
     """
     self._start_relational_field(field)
     related_att = getattr(obj, field.get_attname())
     if related_att is not None:
         if self.use_natural_foreign_keys and hasattr(
                 field.remote_field.model, 'natural_key'):
             related = getattr(obj, field.name)
             # If related object has a natural key, use it
             related = related.natural_key()
             # Iterable natural keys are rolled out as subelements
             for key_value in related:
                 self.xml.startElement("natural", {})
                 self.xml.characters(smart_text(key_value))
                 self.xml.endElement("natural")
         else:
             self.xml.characters(smart_text(related_att))
     else:
         self.xml.addQuickElement("None")
     self.xml.endElement("field")
Beispiel #6
0
 def force_text_recursive(cls, value):
     if isinstance(value, six.string_types):
         return smart_text(value)
     elif isinstance(value, list):
         return [cls.force_text_recursive(x) for x in value]
     elif isinstance(value, tuple):
         return tuple(cls.force_text_recursive(x) for x in value)
     elif isinstance(value, set):
         return set(cls.force_text_recursive(x) for x in value)
     elif isinstance(value, dict):
         return {
             cls.force_text_recursive(k): cls.force_text_recursive(v)
             for k, v in value.items()
         }
     return value
Beispiel #7
0
 def __str__(self):
     return smart_text(self.name or '')
Beispiel #8
0
 def __str__(self):
     return "%s.%s" % (smart_text(
         self.app_label), smart_text(self.model_name))
Beispiel #9
0
 def handle_m2m(value):
     self.xml.addQuickElement(
         "object",
         attrs={'pk': smart_text(value._get_pk_val())})