【问题标题】:Django related model not updating on form submissionDjango 相关模型未在表单提交时更新
【发布时间】:2021-04-22 06:26:52
【问题描述】:

如何通过 Django 模型表单更新不存在的相关对象?

我有两个对象:Participant 和 Emergency。紧急情况是参与者的孩子,例如运行查询:participant = ParticipantInfo.objects.get(pk = prk)我可以访问emergency = participant.emergency

我无法使用 POST 请求使用表单中的数据更新紧急情况。 谁能帮助我。 谢谢

为了清楚起见,这是我的 models.py。

models.py

class EmergencyInfo(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    phone_number = models.CharField(max_length=50)
    email = models.EmailField(max_length=100, blank=True, verbose_name="Emergency Contact Email")
    relationship = models.CharField(max_length=100)


class ParticipantInfo(models.Model):
    first_name = models.CharField(max_length=100)
    middle_initial = models.CharField(max_length=1, blank=True)
     emergency = models.ForeignKey(EmergencyInfo, on_delete = models.CASCADE, editable= False, null=True, blank=True)

views.py

def update_participant(request, pk):
    # this function comes after update_specific 
    if request.method == "GET":
        forms = get_participant_form_data(pk)
        context = {'forms': forms, 'pk': pk}
        return render(request, 'core/participantinfo_update_form.html', context)
     if request.method == "POST":
        return update_specific_form(request, pk)


def update_specific_form(request, pk):
    participant = ParticipantInfo.objects.get(pk = pk)
    
    # if the object didn't exist create it like normal      
    if participant.emergency is None:
        emergencyform =EmergencyForm(request.POST)
        if (emergencyform.is_valid):
            emergencyform.save()
            messages.success(request, 'saved')
            return redirect(request.path_info)
    # if the object exists, update it
    if participant.emergency is not None:
        emergencyform = EmergencyForm(request.POST, instance = participant.emergency)           
        if (emergencyform.is_valid):
            emergencyform.save()
            messages.success(request, 'saved')
            return redirect(request.path_info)

【问题讨论】:

    标签: python django django-views django-forms


    【解决方案1】:

    您的问题似乎与is_valid 方法有关。将其替换为is_valid()。所以你的台词是:

    if (emergencyform.is_valid()):
        #code
    

    【讨论】:

    【解决方案2】:

    我找到了答案。我不仅需要调用is_valid() 而不是is_valid 但我还需要将创建的对象与其父对象联系起来:

    • 保存新的子对象

    • 保存父对象(更新 null 值的外键)

       *#get the existing parent object*
       participant = ParticipantInfo.objects.get(pk = pk)
      
       *#if no child object exists*
      
       if participant.emergency is None:
                   emergencyform =EmergencyForm(request.POST)
                   if (emergencyform.is_valid()):
                       emergency = emergencyform.save(commit=False)
                       participant.emergency  = emergency
                       emergencyform.save()
                       participant.save()
                       messages.success(request, 'saved')
                       return redirect(request.path_info)    
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 2013-07-31
      相关资源
      最近更新 更多