0

当我更改模型中的两个字段时,我收到错误bool对象没有属性。我尝试使用 django-smart-select.( )startswithLocationChainedForeignKey

 from smart_selects.db_fields import ChainedForeignKey 
 class ListOfContinents(models.Model):

    id_Continent = models.AutoField(primary_key=True)
    Continent = models.CharField(max_length=200,unique=True) 

    def __str__(self):
        return '%s' % self.Continent

class ListOfCountries(models.Model):

    id_Country = models.AutoField(primary_key=True)
    Continent = models.ForeignKey(
        ListOfContinents, default=0, to_field='Continent',db_column='Continent')
    Country=models.CharField(max_length=200,unique=True)

    def __str__(self):
        return '%s' % self.Country



class Location(models.Model):

    #Continent=models.CharField(max_length=200,null=True, verbose_name='Континент')
    #Country=models.CharField(max_length=200,null=True, verbose_name='Страна')
    Continent=models.ForeignKey(
        ListOfContinents,to_field='Continent',
        db_column='Continent',verbose_name='Континент',null=True)
    Country = ChainedForeignKey(ListOfCountries,chained_field="Continent",
        chained_model_field="Continent",
        show_all=False,
        auto_choose=True,
        sort=True, 
        verbose_name='Страна',
        db_column='Country',
        to_field='Country',
        null=True)

表格.py

class LocationForm (forms.ModelForm):
    class Meta:
        model = Location
        fields = ('Continent','Country')

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_id = 'id-unitForm'
        # self.helper.form_class = 'blueForms'
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-1'
        self.helper.field_class = 'col-lg-4'
        self.helper.form_method = 'post'
        # self.helper.form_action = 'submit_survey'

        self.helper.add_input(Submit('submit', 'Submit', css_class='btn btn-primary'))
        self.helper.add_input(Button('cancel', 'Cancel', css_class='btn-default', onclick="window.history.back()"))

        super(LocationForm, self).__init__(*args, **kwargs)

完整的错误文本:

'bool' 对象没有属性 'startswith' site-packages\crispy_forms\templates\bootstrap3\uni_form.html,第 4 行错误

第 4 行:

{% if include_media %}
{{ form.media }}
{% endif %}

请帮我解决这些错误。谢谢!

class Media(object):
    def __init__(self, media=None, **kwargs):
        if media:
            media_attrs = media.__dict__
        else:
            media_attrs = kwargs

        self._css = {}
        self._js = []

        for name in MEDIA_TYPES:
            getattr(self, 'add_' + name)(media_attrs.get(name, None))

        # Any leftover attributes must be invalid.
        # if media_attrs != {}:
        #     raise TypeError("'class Media' has invalid attribute(s): %s" % ','.join(media_attrs.keys()))

    def __str__(self):
        return self.render()

    def render(self):
        return mark_safe('\n'.join(chain(*[getattr(self, 'render_' + name)() for name in MEDIA_TYPES])))

    def render_js(self):
        return [format_html('<script type="text/javascript" src="{0}"></script>', self.absolute_path(path)) for path in self._js]

    def render_css(self):
        # To keep rendering order consistent, we can't just iterate over items().
        # We need to sort the keys, and iterate over the sorted list.
        media = sorted(self._css.keys())
        return chain(*[
            [format_html('<link href="{0}" type="text/css" media="{1}" rel="stylesheet" />', self.absolute_path(path), medium)
             for path in self._css[medium]]
            for medium in media])

    def absolute_path(self, path, prefix=None):
        if path.startswith(('http://', 'https://', '/')):
            return path
        if prefix is None:
            if settings.STATIC_URL is None:
                # backwards compatibility
                prefix = settings.MEDIA_URL
            else:
                prefix = settings.STATIC_URL
        return urljoin(prefix, path)

    def __getitem__(self, name):
        "Returns a Media object that only contains media of the given type"
        if name in MEDIA_TYPES:
            return Media(**{str(name): getattr(self, '_' + name)})
        raise KeyError('Unknown media type "%s"' % name)

    def add_js(self, data):
        if data:
            for path in data:
                if path not in self._js:
                    self._js.append(path)

    def add_css(self, data):
        if data:
            for medium, paths in data.items():
                for path in paths:
                    if not self._css.get(medium) or path not in self._css[medium]:
                        self._css.setdefault(medium, []).append(path)

    def __add__(self, other):
        combined = Media()
        for name in MEDIA_TYPES:
            getattr(combined, 'add_' + name)(getattr(self, '_' + name, None))
            getattr(combined, 'add_' + name)(getattr(other, '_' + name, None))
        return combined
4

0 回答 0