Пример #1
0
def fields_list(model, field_names):
    """
    Return a set with the names of the specified fields, checking
    whether each of them exists.

    Arguments: `model` is any subclass of `django.db.models.Model`. It
    may be a string with the full name of a model
    (e.g. ``"myapp.MyModel"``).  `field_names` is a single string with
    a space-separated list of field names.

    If one of the names refers to a dummy field, this name will be ignored
    silently.

    For example if you have a model `MyModel` with two fields `foo` and
    `bar`, then ``dd.fields_list(MyModel,"foo bar")`` will return
    ``['foo','bar']`` and ``dd.fields_list(MyModel,"foo baz")`` will raise
    an exception.

    TODO: either rename this to `fields_set` or change it to return an
    iterable on the fields.
    """
    lst = set()
    names_list = field_names.split()

    for name in names_list:
        if name == '*':
            explicit_names = set()
            for name in names_list:
                if name != '*':
                    explicit_names.add(name)
            for de in wildcard_data_elems(model):
                if not isinstance(de, DummyField):
                    if de.name not in explicit_names:
                        if use_as_wildcard(de):
                            lst.add(de.name)
        else:
            e = model.get_data_elem(name)
            if e is None:
                raise models.FieldDoesNotExist(
                    "No data element %r in %s" % (name, model))
            if not hasattr(e, 'name'):
                raise models.FieldDoesNotExist(
                    "%s %r in %s has no name" % (e.__class__, name, model))
            if isinstance(e, DummyField):
                pass
            else:
                lst.add(e.name)
    return lst
Пример #2
0
 def get_list_field_value(self, name, obj):
     """
     Returns the displayable value for a field specified in
     list_display.
     """
     value = ""
     assert (name in self.list_display)
     try:
         field = obj._meta.get_field(name)
         try:
             if len(field.get_choices()) > 0 and hasattr(
                     obj, 'get_' + name + '_display'):
                 value = getattr(obj, 'get_' + name + '_display')()
             else:
                 value = getattr(obj, name)
         except AttributeError:
             value = getattr(obj, name)
     except models.FieldDoesNotExist:
         from django.utils.safestring import mark_safe
         if hasattr(self.get_model(), name):
             value = getattr(obj, name)
         elif hasattr(self, name):
             value = getattr(self, name)(obj)
         else:
             raise models.FieldDoesNotExist("Could not evaluate column '" +
                                            name + "'")
         if type(value) != type(True):
             value = mark_safe(value)
     '''
     if type(value) == datetime:
         value = formats.date_format(value, "SHORT_DATETIME_FORMAT")
     if type(value) == bool:
         return BooleanValueFormatter().format(value)
     '''
     return self.format_value(value)
Пример #3
0
 def email(self):
     try:
         return operator.attrgetter(self.BUYER_EMAIL_RELATION)(self)
     except AttributeError:
         raise models.FieldDoesNotExist(
             f'Buyer model should be linked to an email using relation {self.BUYER_EMAIL_RELATION} '
             f'set in {self._BUYER_EMAIL_RELATION_SETTING}'
         )
Пример #4
0
def fields_list(model, field_names):
    """Return a set with the names of the specified fields, checking whether
    each of them exists.

    Arguments: `model` is any subclass of `django.db.models.Model`. It
    may be a string with the full name of a model
    (e.g. ``"myapp.MyModel"``).  `field_names` is a single string with
    a space-separated list of field names.

    If one of the names refers to a :class:`DummyField`, this name
    will be ignored silently.

    For example if you have a model `MyModel` with two fields `foo` and
    `bar`, then ``dd.fields_list(MyModel,"foo bar")`` will return
    ``['foo','bar']`` and ``dd.fields_list(MyModel,"foo baz")`` will raise
    an exception.

    """
    lst = set()
    if '*' in field_names:
        explicit_names = set()
        for name in field_names.split():
            if name != '*':
                explicit_names.add(name)
        wildcard_names = [
            de.name for de in wildcard_data_elems(model)
            if (de.name not in explicit_names) and use_as_wildcard(de)
        ]
        wildcard_str = ' '.join(wildcard_names)
        field_names = field_names.replace('*', wildcard_str)

    for name in field_names.split():

        e = model.get_data_elem(name)
        if e is None:
            raise models.FieldDoesNotExist("No data element %r in %s" %
                                           (name, model))
        if not isinstance(e, DummyField):
            lst.add(e.name)
    return lst