Django - 提交表单后更新一个字段



我正在寻找在用户提交后更新我的 Django 表单。我用一些数据填充了我的BirthCertificateForm,我只有一个字段没有填充:social_number

为什么?因为在提交表单后,我从数据表单创建了一个唯一的社交号码。因此,我想更新我以前的表单,并根据良好字段添加此社交号码并进行验证。

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

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)

我的 views.py 文件有 2 个功能:

第一个功能:

# First function which let to fill the form with some conditions / restrictions
@login_required
def BirthCertificate_Form(request) :
    # Fonction permettant de créer le formulaire Acte de Naissance et le remplissage

    query_lastname_father = request.GET.get('lastname_father')
    query_lastname_mother = request.GET.get('lastname_mother')
    if request.method == 'POST':
        form = BirthCertificateForm(request.POST or None)
        if form.is_valid() :   # Vérification sur la validité des données
            post = form.save()
            return HttpResponseRedirect(reverse('BC_treated', kwargs={'id': post.id}))
    else:
        form = BirthCertificateForm()

        parent1 = Person.objects.filter(lastname=query_lastname_father)
        parent2 = Person.objects.filter(lastname=query_lastname_mother)
        form = BirthCertificateForm(request.POST or None)
        form.fields['fk_parent1'].queryset = parent1.filter(sex="Masculin")
        form.fields['fk_parent2'].queryset = parent2.filter(sex="Feminin")
    context = {
        "form" : form,
        "query_lastname" : query_lastname_father,
        "query_lastname_mother" : query_lastname_mother,
    }
    return render(request, 'BC_form.html', context)

第二个功能:

# Second function which resume the previous form and create my social number
@login_required
def BirthCertificate_Resume(request, id) :
    birthcertificate = get_object_or_404(BirthCertificate, pk=id)
    #Homme = 1 / Femme = 2
    sex_number = []
    if birthcertificate.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(birthcertificate.birthday.year)
    birthyear_temp2 = str(birthyear_temp.split(" "))
    birthyear = birthyear_temp2[4] + birthyear_temp2[5]
    #Récupère mois de naissance
    birthmonth_temp = birthcertificate.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 = birthcertificate.mairie
    print birth_mairie
    #Génère un nombre aléaloire :
    key_temp = randint(0,999)
    if len(str(key_temp)) == 1 :
        key = '00' + str(key_temp)
    elif len(str(key_temp)) ==2 :
        key = 'O' + 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
    return render(request, 'BC_resume.html', {"birthcertificate" : birthcertificate})
我的

问题是:我如何再次获取我的表单并对其进行修改以填充刚刚创建的social_number字段?

谢谢!

对于初学者,我建议您参考本文和本文。

现在,根据您要执行的操作,我会在您的 models.py 中编写定义模型的post_save。因此,这就是它的样子:

from django.db import models
from django.dispatch import receiver
from django.db.models.signals import post_save

class BirthCertificate(models.Model):
    ...  # your model attributes

# Here is where the post_save hook happens
@receiver(post_save, sender=BirthCertificate, dispatch_uid='generate_social_number')  # You can name the dispatch_uid whatever you want
def gen_social(sender, instance, **kwargs):
    if kwargs.get('created', False):
        # Auto-generate the social number
        # This is where you determine the sex number, birth year, birth month, etc. that you have calculated in your views.py.
        # Use the instance object to get all the model attributes.
        # Example: I'll calculate the sex number for you, but I leave the other calculations for you to figure out.
        if instance.sex == 'Masculin':
            sex_number = 1
        else:
            sex_number = 2
        ...  # Calculate the other variables that you will use to generate your social number
        ...  # More lines to create the variables
        instance.social_number = str(sex_number) + ...  # Now set the instance's social number with all the variables you calculated in the above steps
        instance.save()  # Finally your instance is saved again with the generated social number

注意:我假设您想在新创建出生证明记录时生成social_number,而不是在修改现有记录时生成。这就是为什么我使用if kwargs.get('created', False)条件。此条件基本上检查您是创建新记录还是修改现有记录。如果希望此post_save即使在修改记录后也能运行,请删除该条件。

我找到了一个可以更新我的模型的解决方案。

我有这个功能:

@login_required
def BirthCertificate_Resume(request, id) :
    birthcertificate = get_object_or_404(BirthCertificate, pk=id)
    #Homme = 1 / Femme = 2
    sex_number = []
    if birthcertificate.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(birthcertificate.birthday.year)
    birthyear_temp2 = str(birthyear_temp.split(" "))
    birthyear = birthyear_temp2[4] + birthyear_temp2[5]
    #Récupère mois de naissance
    birthmonth_temp = birthcertificate.birthday.month
    if len(str(birthmonth_temp)) == 1 :
        birthmonth = '0' + str(birthmonth_temp)
    else :
        birthmonth = birthmonth_temp
    # #Récupère première lettre nom 
    # lastname_temp = birthcertificate.lastname
    # lastname = lastname_temp[0]
    # print lastname
    # #Récupère première lettre prénom 
    # firstname_temp = birthcertificate.firstname
    # firstname = firstname_temp[0]
    # print firstname

    #Récupère N° Mairie (ici récupère nom mais sera changé en n°)
    birth_mairie = birthcertificate.mairie
    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

    return render(request, 'BC_resume.html', {"birthcertificate" : birthcertificate, "social_number" : social_number})

所以我只添加两行,它似乎工作得很好:

birthcertificate.social_number = social_number
birthcertificate.save()

您如何看待这种方法?

最新更新