我花了一整天和今晚为了解决这个错误:
AttributeError at /Identity/formulaire_edit
'unicode' object has no attribute '_meta'
Request Method: GET
Request URL: http://localhost:8000/Identity/formulaire_edit?csrfmiddlewaretoken=DvKCCaaAYsiWykEtYHrpgNu4AKlXS16DttEc5csQOT9BtQs4Ll4AviLDhi3MaBId&q4=2
Django Version: 1.10
Exception Type: AttributeError
Exception Value:
'unicode' object has no attribute '_meta'
Exception Location: /Library/Python/2.7/site-packages/django/forms/models.py in model_to_dict, line 82
Python Executable: /usr/bin/python
Python Version: 2.7.10
Python Path:
['/Users/valentinjungbluth/Desktop/Django/Etat_civil',
'/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
Server time: jeu, 1 Déc 2016 07:48:15 +0000
Traceback Switch to copy-and-paste view
/Library/Python/2.7/site-packages/django/core/handlers/exception.py in inner
response = get_response(request) ...
▶ Local vars
/Library/Python/2.7/site-packages/django/core/handlers/base.py in _get_response
response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
/Library/Python/2.7/site-packages/django/core/handlers/base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/Users/valentinjungbluth/Desktop/Django/Etat_civil/Identity/views.py in Identity_Update
form = IdentityForm(request.POST or None, instance = query_update)
我无法使用我的函数,我认为这是因为尝试值应该是一个原始整数,但我需要把这个变量放在一个整数上。这是用户返回的值:
# models.py
#-*- coding: utf-8 -*-
from django.db import models
######################################
# Choix à l'utilisateur pour le sexe #
######################################
SEX_CHOICES = (
('M', 'Mâle'),
('F', 'Femelle')
)
##########################################
# Choix à l'utilisateur pour la civilité #
##########################################
TITLE_CHOICES = (
('Mr', 'Monsieur'),
('Mlle', 'Mademoiselle'),
('Mme','Madame'),
('Dr','Docteur'),
('Me','Maître'),
)
###################################################
# Création d'une table répertoriant tous les pays #
###################################################
class Country(models.Model):
code = models.CharField(max_length=3, null=False) # Example : 'FR' - 'US'
pays = models.CharField(max_length=50, null=False) # Example : 'France' - 'Etats-Unis'
def __unicode__(self):
#return '%s %s %s' % (self.id, self.code, self.pays)
return self.pays
####################################################################################
# Création d'une table permettant de renseigner toutes les informations concernant #
# les parents et reprise de celles des enfants #
####################################################################################
class Identity(models.Model):
title = models.CharField(max_length=12,choices=TITLE_CHOICES, verbose_name='Civilité')
lastname = models.CharField(max_length=30, verbose_name='Nom de famille')
firstname = models.CharField(max_length=30, verbose_name='Prénom(s)')
sex = models.CharField(max_length=1, choices=SEX_CHOICES, verbose_name='sexe')
birthday = models.DateField(verbose_name='Date de naissance')
birthcity = models.CharField(max_length=30, verbose_name='Ville de naissance')
birthcountry = models.ForeignKey(Country, related_name='Pays_naissance', verbose_name='Pays de Naissance')
nationality = models.CharField(max_length=30, verbose_name='Nationalité')
job = models.CharField(max_length=30, verbose_name='Profession')
adress = models.CharField(max_length=30, verbose_name='Adresse')
city = models.CharField(max_length=30, verbose_name='Ville')
zip = models.IntegerField(verbose_name='Code Postal')
country = models.ForeignKey(Country, related_name='Pays1', verbose_name='Pays')
mail = models.CharField(max_length=30, verbose_name='Email', blank=True)
phone = models.CharField(max_length=20, verbose_name='Téléphone', blank=True)
def __unicode__(self):
return '%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s' % (self.id, self.title, self.lastname, self.firstname, self.sex, self.birthday, self.birthcity, self.birthcountry,
self.nationality, self.job, self.adress, self.city, self.zip, self.country, self.mail, self.phone)
我的forms.py文件:
#-*- coding: utf-8 -*-
from django import forms
from .models import *
class IdentityForm(forms.ModelForm) :
class Meta :
model = Identity
fields = '__all__'
关于Identity_Update()
函数的我的views.py文件:
def Identity_Update(request) :
query_update = request.GET.get('q4')
print query_update
if query_update :
query_update_list = Identity.objects.filter(pk=query_update) #Identity.objects.only('id').filter(pk=query_update) to get only the ID number
print query_update_list
else :
query_update_list = Identity.objects.none() # == []
form = IdentityForm(request.POST or None, instance = query_update)
if form.is_valid():
form.save()
return HttpResponseRedirect('accueil')
template_name = 'edit.html'
context = {
"query_update" : query_update,
"query_update_list" : query_update_list,
"form":form
}
return render(request, template_name, context)
这是我的html模板:
<!-- ############################################## -->
<!-- Modifier un formulaire dans la Base de Données -->
<!-- ############################################## -->
<h2 align="center"> Modification des fiches individuelles </align> </h2>
{% block content %}
<h4> ID du formulaire à modifier : </h4>
<form method="GET" action="">{% csrf_token %}
<input type="text" name="q4" placeholder="Entrer un ID" value="{{ request.GET.q4 }}">
<input type="submit" value="Valider l'ID">
</form>
<ul>
{% for item in query_update_list %}
<li>{{item}}</li>
{% endfor %}
</ul>
<br></br>
<h4> Modification du formulaire : </h4>
<form method='POST' action= "" > {% csrf_token %}
{{form.as_ul}}
<input type ="submit" value="Valider" />
</form>
{% endblock %}
你能帮我找一个解决方案吗?我从昨天起就阻止了这个问题。
答案 0 :(得分:1)
问题是你传入了一个实例的字符串(query_update
)。
form = IdentityForm(request.POST or None, instance = query_update)
相反,您需要传入表单应该更新的单个实例
query_update_list.first()
可能会得到你需要的那个。