python - Django错误: hasattr(): attribute name must be string

标签 python django

我收到 Django/Pythonic 错误,但我不知道如何解决它。我尝试了很多方法,但到目前为止错误仍然存​​在。

我的错误是:

hasattr(): attribute name must be string

这个错误来 self 的 Django 表单。当我点击验证表单时,我遇到了这个问题。

我的models.py 文件看起来像:

class Individu(models.Model):

    NumeroIdentification = models.CharField(max_length=30, null=True, verbose_name='Numéro Identification physique', unique=True)
    Civilite = models.CharField(max_length=12,choices=CHOIX_TITRE, verbose_name='Civilité')
    NomJeuneFille = models.CharField(max_length=30, verbose_name='Nom de jeune fille', blank=True)
    Nom = models.CharField(max_length=30, verbose_name='Nom de famille')
    Prenom = models.CharField(max_length=30, verbose_name='Prénom(s)')
    Sexe = models.CharField(max_length=30, choices=CHOIX_SEXE, verbose_name='Sexe')
    Statut = models.CharField(max_length=30, choices=CHOIX_STATUT, verbose_name="Statut civil")
    DateNaissance = models.DateField(verbose_name='Date de naissance')
    VilleNaissance = models.CharField(max_length=30, verbose_name='Ville de naissance')
    PaysNaissance = CountryField(blank_label='Sélectionner un pays', verbose_name='Pays de naissance')
    Nationalite1 = models.CharField(max_length=30, verbose_name='Nationalité 1')
    Nationalite2 = models.CharField(max_length=30, verbose_name='Nationalité 2', null=True, blank=True)
    Profession = models.CharField(max_length=30, verbose_name='Profession')
    Adresse = models.CharField(max_length=30, verbose_name='Adresse')
    Ville = models.CharField(max_length=30, verbose_name='Ville')
    Zip = models.IntegerField(verbose_name='Code Postal')
    Pays = CountryField(blank_label='Sélectionner un pays', verbose_name='Pays')
    Mail = models.CharField(max_length=30, verbose_name='Email', blank=True)
    Telephone = models.CharField(max_length=20, verbose_name='Téléphone', blank=True)
    Creation = models.DateTimeField(auto_now_add=True)
    InformationsInstitution = models.CharField(max_length=30, null=False, verbose_name='Informations Institution')
    Utilisateur = models.CharField(max_length=100, null=False, verbose_name="Utilisateur", default=" ")
    Etat = models.CharField(max_length=30, choices=CHOIX_ETAT, default=" ", null=False, verbose_name="Etat")

    def save(self, *args, **kwargs):
        for field_name in ['NomJeuneFille' ,'Nom', 'VilleNaissance', 'Nationalite1', 'Nationalite2', 'Ville', 'Profession']:
            val = getattr(self, field_name, False)
            if val:
                setattr(self, field_name, val.upper())

        for field_name in ['Prenom']:
            val = getattr(self, field_name, False)
            if val:
                new_val = []
                words = val.split()
                for x in words:
                    x = x.capitalize()
                    new_val.append(x)
                val = " ".join(new_val)
                setattr(self, field_name, val.capitalize())

        super(Individu, self).save(*args, **kwargs)


    def __unicode__(self):
        return '%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s' % (self.id, self.NumeroIdentification, self.Civilite, self.NomJeuneFille ,self.Nom, self.Statut, self.Prenom, 
                                                                            self.Sexe, self.DateNaissance, self.VilleNaissance, self.PaysNaissance, self.Nationalite1, self.Nationalite2, 
                                                                            self.Profession, self.Adresse, self.Ville, self.Zip, self.Pays, self.Mail, self.Telephone, self.Etat, self.InformationsInstitution)

然后,我有了我的 form.py 文件:

class IndividuFormulaire(forms.ModelForm) :

    InformationsInstitution = forms.CharField(widget=forms.HiddenInput(), initial=InformationsInstitution.objects.last().Ville.encode('utf-8'))
    Utilisateur = forms.CharField(widget=forms.HiddenInput())

    class Meta :
        model = Individu
        fields = [
                'Etat', 
                'Utilisateur', 
                'Civilite', 
                'NomJeuneFille',
                'Prenom', 
                'Nom', 
                'Statut', 
                'Sexe', 
                'DateNaissance', 
                'VilleNaissance', 
                'PaysNaissance', 
                'Nationalite1', 
                'Nationalite2',
                'Profession', 
                'Adresse', 
                'Ville', 
                'Zip', 
                'Pays', 
                'Mail', 
                'Telephone', 
                'InformationsInstitution',]

还有我的views.py函数:

@login_required
def Identity_Individu_Form(request) :

    success = False

    if request.method == 'POST':

        form = IndividuFormulaire(request.POST or None)

        if form.is_valid() :  
            post = form.save()
            messages.success(request, 'Le formulaire a été enregistré !')
            return HttpResponseRedirect(reverse('IndividuResume', kwargs={'id': post.id}))

        else:
            messages.error(request, "Le formulaire est invalide !")

    else:
        form = IndividuFormulaire()
        form.fields['Utilisateur'].initial = request.user.last_name + " " + request.user.first_name
    return render(request, 'Identity_Individu_Form.html', {"form" : form})

数据库有一个特殊性。我使用 Django routers 是因为我需要将表单结果保存在多个数据库中,正如您在这张图片中看到的那样。我必须将其保存在 DS_CORE.Identity_individuDS_PUBLIC.Identity_individu 中。

enter image description here

所以我的 GlobalRouter.py 文件中有:

class GlobalRouter(object):
    """
A router to control all database operations on models in the
auth application.
"""

    def db_for_read(self, model, **hints):
        """
        Attempts to read auth models go to auth.
        """
        app_list = ('Identity')

        if model._meta.app_label in app_list:
            return 'default'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth models go to auth.
        """
        app_list = ('Identity')
        if model._meta.app_label in app_list:
            return ('DS_Public','default')
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the auth app is involved.
        """
        app_list = ('Identity')
        if obj1._meta.app_label in app_list and obj2._meta.app_label in app_list:
            return True
        return None

    def allow_migrate(self, db, app_label, model=None, **hints):
        """
        Make sure the auth app only appears in the 'auth'
        database.
        """
        app_list = ('Identity')

        if app_label in app_list:
            return db == ('DS_Public','default')
        return None

我没有找到错误所在。我正在加入完整的回溯:

Environment:


Request Method: POST
Request URL: http://localhost:8000/Identity/Formulaire/Individus

Django Version: 1.10.3
Python Version: 2.7.12
Installed Applications:
['Informations',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'bootstrapform',
 'django_countries',
 'debug_toolbar',
 'chartit',
 'Configurations',
 'Accueil',
 'log',
 'Identity']
Installed Middleware:
['django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.gzip.GZipMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware']



Traceback:

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/Users/valentinjungbluth/Desktop/Django/DatasystemsCORE/DatasystemsCORE/Identity/views.py" in Identity_Individu_Form
  140.             post = form.save()

File "/usr/local/lib/python2.7/site-packages/django/forms/models.py" in save
  453.             self.instance.save()

File "/Users/valentinjungbluth/Desktop/Django/DatasystemsCORE/DatasystemsCORE/Identity/models.py" in save
  116.         super(Individu, self).save(*args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py" in save
  796.                        force_update=force_update, update_fields=update_fields)

File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
  821.         with transaction.atomic(using=using, savepoint=False):

File "/usr/local/lib/python2.7/site-packages/django/db/transaction.py" in __enter__
  152.         connection = get_connection(self.using)

File "/usr/local/lib/python2.7/site-packages/django/db/transaction.py" in get_connection
  21.     return connections[using]

File "/usr/local/lib/python2.7/site-packages/django/db/utils.py" in __getitem__
  205.         if hasattr(self._connections, alias):

Exception Type: TypeError at /Identity/Formulaire/Individus
Exception Value: hasattr(): attribute name must be string

最佳答案

db_for_writedb_for_read 应返回与连接别名对应的字符串。在您的情况下,db_for_write有时会返回一个元组

关于python - Django错误: hasattr(): attribute name must be string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44679743/

相关文章:

python - 使用Python Paramiko在后台运行远程SSH服务器的过程

python - pyaudio在python2和python3中播放不同的音调

python - 为什么我的 Django request.method 是 'GET' 而不是 'POST'?

python - 使用 postgresql 数据库时 uwsgi 下的 django 应用程序无法启动

Python 神经网络和股票价格 : What to use for input?

python - 带颜色条的圆图

Python -- Pygame 属性错误 : int object has no attribute 'draw'

django - "first time here?"消息与 django

python - django模板嵌套字典,按键获取值

django - 使用Django+Redis+Socket.io搭建聊天室,从何入手?