Beispiel #1
0
    def get_fields(cls, fields=None, excludes=None):
        """
        Given any explicit fields to include and fields to exclude, add
        additional fields based on the associated form.
        """

        final_fields = {}
        fields = fields or []
        excludes = excludes or []

        if not cls._meta.object_class:
            return final_fields

        for name, field in six.iteritems(cls._meta.object_class.base_fields):

            if name in cls.base_fields:
                continue

            if fields and name not in fields:
                continue

            if excludes and name in excludes:
                continue

            if cls.should_skip_field(name):
                continue

            api_field_class = cls.api_field_from_django_field(field)

            kwargs = {
                "attribute": name,
                "help_text": field.help_text
            }

            if field.required is False:
                kwargs["null"] = True

            if cls.get_internal_type(field) == "TextField":
                kwargs["default"] = ""

            if field.initial:
                kwargs["default"] = field.initial

            final_fields[name] = api_field_class(**kwargs)
            final_fields[name].instance_name = name

        f = RemoteForm(cls._meta.object_class())
        f.as_dict()

        return final_fields
Beispiel #2
0
    def full_dehydrate(self, bundle, for_list=False):
        """
        Given a bundle with an object instance, extract the
        information from it to populate the resource.

        Basically, this means to serialize the form to json.
        """
        remote_form = RemoteForm(bundle.obj)
        bundle.data.update(remote_form.as_dict())
        return bundle