我尝试将两个Django models
与一些常见字段(lastname
,firstname
,....)进行比较,以便在答案为&时应用流程#34;是"或"不"。
我有两种模式:
lastname
,firstname
,birthday
等字段为人们提供身份识别,...)firstname
,lastname
,...)使用这两个模型,我有共同的字段,我会比较这些字段,以便应用两个不同的过程。
我的模特看起来像:
class Person(models.Model):
social_number = models.CharField(max_length=30, null=True, verbose_name='numero social')
title = models.CharField(max_length=12,choices=TITLE_CHOICES, verbose_name='Civilité')
young_girl_lastname = models.CharField(max_length=30, verbose_name='Nom de jeune fille', blank=True)
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=8, choices=SEX_CHOICES, verbose_name='Sexe')
status = models.CharField(max_length=15, choices=STATUS_CHOICES, verbose_name="Statut civil")
birthday = models.DateField(verbose_name='Date de naissance')
birthcity = models.CharField(max_length=30, verbose_name='Ville de naissance')
birthcountry = CountryField(blank_label='Sélectionner un pays', verbose_name='Pays de naissance')
birthmairie = models.CharField(max_length=30, verbose_name='Mairie 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 = CountryField(blank_label='Sélectionner un pays', 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)
created = models.DateTimeField(auto_now_add=True)
mairie = models.CharField(max_length=30, null=False, verbose_name='Mairie', default=' ')
和
class BirthCertificate(models.Model):
lastname = models.CharField(max_length=30, null=False, verbose_name='Nom de famille')
firstname = models.CharField(max_length=30, null=False, verbose_name='Prénom(s)')
sex = models.CharField(max_length=8, choices=SEX_CHOICES, verbose_name='Sexe')
birthday = models.DateField(null=False, verbose_name='Date de naissance')
birthhour = models.TimeField(null=True, verbose_name='Heure de naissance')
birthcity = models.CharField(max_length=30, null=False, verbose_name='Ville de naissance')
birthcountry = CountryField(blank_label='Sélectionner un pays', verbose_name='Pays de naissance')
fk_parent1 = models.ForeignKey(Person, related_name='ID_Parent1', verbose_name='ID parent1', null=False)
fk_parent2 = models.ForeignKey(Person, related_name='ID_Parent2', verbose_name='ID parent2', null=False)
mairie = models.CharField(max_length=30, null=False, verbose_name='Mairie')
social_number = models.CharField(max_length=30, null=True, verbose_name='numero social')
created = models.DateTimeField(auto_now_add=True)
第一种情况:
假设Person
和BirthCertificate
之间有同一个人。为此,我们会比较4个字段:lastname
,firstname
,birthday
,birthcity
。
如果确实如此,我会选择BirthCertificate.social_number
并按先前的值更新我的字段Person.social_number
。
如果错误,我会在social_number
中创建一个新的Person
,然后用新的更新Person.social_number
。
通常,真正的可能性对应于新生儿。首先我们制作一个BirthCertificate然后我们创建一个包含更多细节的IdentityCertificate。错误的可能性对应于没有BirthCertificate的成年人。
最后,目标是为每个人提供一个独特的社交号码。
我的功能如下:
@login_required
def Identity_Resume(request, id) :
# Form is validated with an empty `social_number` field
# I pick up data previously created
person = get_object_or_404(Person, pk=id)
blastname = BirthCertificate.objects.all().values("lastname")
bfirstname = BirthCertificate.objects.all().values("firstname")
bbirthday = BirthCertificate.objects.all().values("birthday")
bbirthcity = BirthCertificate.objects.all().values("birthcity")
if person.lastname == blastname and person.firstname == bfirstname and person.birthday == bbirthday and person.birthcity == bbirthcity :
# If all fields are identicals, I pick up the `social_number` from the true BirthCertificate id
else :
#Homme = 1 / Femme = 2
sex_number = []
if person.sex == 'Masculin' :
sex_number = 1
print sex_number
else :
sex_number = 2
print sex_number
#Récupère année de naissance
birthyear_temp = str(person.birthday.year)
birthyear_temp2 = str(birthyear_temp.split(" "))
birthyear = birthyear_temp2[4] + birthyear_temp2[5]
#Récupère mois de naissance
birthmonth_temp = person.birthday.month
if len(str(birthmonth_temp)) == 1 :
birthmonth = '0' + str(birthmonth_temp)
else :
birthmonth = birthmonth_temp
#Récupère N° Mairie (ici récupère nom mais sera changé en n°)
birth_mairie = person.birthmairie
print birth_mairie
#Génère un nombre aléaloire :
key_temp = randint(0,999999)
if len(str(key_temp)) == 1 :
key = '00000' + str(key_temp)
elif len(str(key_temp)) == 2 :
key = '0000' + str(key_temp)
elif len(str(key_temp)) == 3 :
key = '000' + str(key_temp)
elif len(str(key_temp)) == 4 :
key = '00' + str(key_temp)
elif len(str(key_temp)) == 5 :
key = '0' + str(key_temp)
else :
key = key_temp
print key
social_number = str(sex_number) + ' ' + str(birthyear) + ' ' + str(birthmonth) + ' ' + str(birth_mairie) + ' - ' + str(key)
print social_number
#Mise à jour du champ numéro sécurité social
person.social_number = social_number
person.save()
context = {
"person" : person,
"social_number" : social_number,
"blastname" : blastname,
"bfirstname" : bfirstname,
"bbirthday" : bbirthday,
"bbirthcity" : bbirthcity,
}
return render(request, 'identity_resume.html', context)
我的问题是:
如何在代码中编写比较部分?
我为许多英语错误道歉,并且很有意思。
谢谢
编辑:
@login_required
def Identity_Resume(request, id) :
# Form is validated with an empty `social_number` field
# I pick up data previously created
person = get_object_or_404(Person, pk=id)
social_number = BirthCertificate.objects.filter(firstname=person.firstname, lastname=person.lastname, birthday=person.birthday, birthcity=person.birthcity).values('social_number')
print social_number
if social_number.exists() :
person.social_number = social_number
person.save()
但我明白了:
<QuerySet [{'social_number': u'2 08 12 STRASBOURG - 039838'}]>
而不仅仅是:
2 08 12 STRASBOURG - 039838
答案 0 :(得分:1)
我认为你不能这样做,因为BirthCertificate.objects.all().values("lastname")
给出ValueQuerySet
所以你可以做到
if BirthCertificate.objects.filter(
firstname=person.firstname,
lastname=person.lastname,
etc..).exists(): #etc means other fields.
<强>更新强>
person = get_object_or_404(Person, pk=id)
obj = BirthCertificate.objects.filter(firstname=person.firstname, lastname=person.lastname, birthday=person.birthday, birthcity=person.birthcity)
if obj:
sc_obj = obj[0] #check if multiple objects are there, means obj[1]
person.social_number = sc_obj.social_number
person.save()