我收到了Django / Pythonic错误,我不知道如何解决它。我尝试了很多东西,但到目前为止还存在错误。
我的错误是:
hasattr():属性名必须是字符串
此错误来自我的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_individu
和DS_PUBLIC.Identity_individu
。
所以我的 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
我找不到错误的位置。我正在加入完整的Traceback:
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
答案 0 :(得分:2)
db_for_write
和db_for_read
应返回与连接别名对应的字符串。在您的情况下,db_for_write
有时会返回一个元组
答案 1 :(得分:0)
看起来你的GlobalRouter方法返回None。
我可以看到的错误是您已将app_list
定义为字符串而非元组。
此代码最终以app_list
为字符串:
app_list = ('Identity')
这段代码是你想要的,这是一个元组:
app_list = ('Identity',)