簡體   English   中英

如何在具有多個表的相同外鍵的 django 中使用帶有外鍵的 Modelform?

[英]How to use Modelform with foreign key in django with same foreign key with multiple tables?

最近我正在使用 django 創建一個簡歷制作網站。 如何使用外鍵將數據從模型表單保存到數據庫。 我想將教育詳細信息與相關用戶信息一起保存在教育表中。 我無法提交教育表格的數據。

這是我的代碼,

模型.py

from django.db import models

# Create your models here.
class Person(models.Model):
    first_name = models.CharField(max_length=25)
    middle_name = models.CharField(max_length=25)
    last_name = models.CharField(max_length=25)
    email = models.EmailField()
    mobile_no = models.IntegerField()
    age = models.IntegerField()
    dob = models.DateField()
    address = models.TextField()
    github = models.URLField(blank=True, null=True)
    linkedin = models.URLField(blank=True, null= True)
    website = models.URLField(blank=True,null = True)

    def full_name(self):
        return " ".join([self.first_name, self.middle_name, self.last_name])

class Education(models.Model):
    Degrees = (
        ('PhD','PhD'),
        ('Mtech/MA/MSc/MCom/MBA','Masters'),
        ('BE/Btech/BA/BSc/BCom','Bachelors'),
        ('12th','High School')
    )
    person = models.ForeignKey(Person,on_delete=models.CASCADE)
    qualification = models.CharField(choices=Degrees,max_length=25)
    institution = models.CharField(max_length=75)
    board = models.CharField(max_length=75)
    start_yr = models.DateField()
    end_yr = models.DateField()
    cgpa = models.FloatField(blank=True,null=True)
    percent = models.FloatField(blank=True, null=True)


Forms.py

from django import forms
from django.forms import ModelForm,modelformset_factory,DateField
from django.conf import settings
from .models import *


class PersonForm(ModelForm):
    class Meta:
        model = Person
        fields = "__all__"

class EducationForm(ModelForm):
    class Meta:
        model = Education
        exclude = ('person',)


視圖.py

from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse
from .forms import *
from .models import *

forms = {
              "person":PersonForm,
              "edu": EducationForm,
}

# Create your views here.
def render_page(request):
    person = Person.objects.last()
    context={
         "person": person,
         "education":Education,
        }

    return render(request,"site.html",context)

def get_input(request):
    if request.method == "POST":
        form = PersonForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = PersonForm()
    return render(request, "getinput.html",{ "form" : form})

def edu(request):
    if request.method == "POST":
        form = EducationForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = EducationForm()
    return render(request,"Education.html",{"edu":form})


首先,您應該找到要保存其詳細信息的人,然后將此 object 添加到表單實例中。 在這個例子中,我使用了 first_name 過濾器。

def edu(request):
    if request.method == "POST":
        form = EducationForm(request.POST)
        # Find the required Person to save their education details
        # Filter using any property
        person = Person.objects.filter(first_name='').first()
        if form.is_valid():
            # Add the person the the form instance
            form.instance.person = person
            form.save()
    else:
        form = EducationForm()
    return render(request,"Education.html",{"edu":form})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM