k ho
k ho

Reputation: 27

Django - Foreign Key Is Not Working Porperly

So basically my template has three columns in one form where they work separately. If I give input for contact column and male column it will save in my database. But the problem is that foreign key is not working.Whenever I include foreign key variable in my fields (forms.py) .Form will not submit my inputs in database but when I remove it from fields though it works but wont show the contact related just none value. Also It works when the default is True in foreign key variable.

As conclusion foreign key is not relating to the male or female model.

Help please:

Here is my model:

class Contact(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    age = models.IntegerField()
    number = models.IntegerField()
    type = models.CharField(max_length=50)
    gender = models.CharField(max_length=10)
    order_date = models.DateField(blank=True,null=True)
    issue_date = models.DateField(blank=True,null=True)
   

    def __str__(self):
        return self.name


class Male(models.Model):
    contact1 = models.ForeignKey(Contact,  on_delete=models.CASCADE,null=True)
    chest = models.CharField(max_length=30 , blank=True)
    neck = models.CharField(max_length=30 , blank=True)
    full_shoulder_width = models.CharField(max_length=30 , blank=True)
    right_sleeve = models.CharField(max_length=30 , blank=True)
    left_sleeve = models.CharField(max_length=30 , blank=True)
    bicep = models.CharField(max_length=30 , blank=True)
    def __str__(self):
        return self.chest

 


class Female(models.Model):
    contact2 = models.ForeignKey(Contact,  on_delete=models.CASCADE, null=True)
    fchest = models.CharField(max_length=30 , blank=True)
    fneck = models.CharField(max_length=30 , blank=True)
    fwaist = models.CharField(max_length=30 , blank=True)
    seat = models.CharField(max_length=30 , blank=True)
    shoulder_width = models.CharField(max_length=30 , blank=True)
    arm_length = models.CharField(max_length=30 , blank=True)

Views.py

def add(request):
    template_name = 'add.html'
    f_form = ''
    c_form = ''
    m_form = ''
    contact = ''
    contact = Contact.objects.all()  
    if request.method =='POST':
        c_form = commentForm(request.POST)
        m_form = maleForm(request.POST)
        f_form = femaleForm(request.POST)
        if c_form.is_valid() and (m_form.is_valid() or f_form.is_valid()):
                gender = c_form.cleaned_data.get('gender')
                username= c_form.cleaned_data.get('name')     
                c_form.save()
                if gender == 'female':
                        c_form.save()
                        f_form.save()
                        messages.success(request, f"Form Submitted: {username}")
                        return redirect("success")
                else:
                        c_form.save()
                        m_form.save()
                        messages.success(request, f"Form Submitted: {username}")
                        return redirect("success")     
        else:     
                    c_form = commentForm()
                    m_form = maleForm()   
                    f_form = femaleForm()
                    
       
              
    context = {
      
        'c_form' : c_form,
        'm_form' : m_form,
        'f_form' : f_form,
        'contact' : contact,

    }

    return render(request , template_name , context)
    

forms.py

from django import forms
from .models import Contact , Male ,Female



class commentForm(forms.ModelForm):
    class Meta:
        model = Contact
        fields = '__all__'
        


class maleForm(forms.ModelForm):
    class Meta:
        model = Male
        
        fields = ('chest' , 'neck' , 'full_shoulder_width' ,'right_sleeve' , 'left_sleeve' ,'bicep' )
        

class femaleForm(forms.ModelForm):
    class Meta:
        model = Female
        fields = ('fchest' , 'fneck' , 'fwaist' ,'seat' , 'shoulder_width' ,'arm_length' )

Upvotes: 1

Views: 837

Answers (1)

Ceetified_karma
Ceetified_karma

Reputation: 368

You need to save your comment form first and save it to a variable so you can use it as the other form foriegn key field

 contact = c_form.save()
 if gender == 'female':
       female = f_form.save(commit=False)
       female.contact2 = contact
       female.save()
       messages.success(request, f"Form Submitted: 
 {username}")
       return redirect("success")
else:
        male = m_form.save(commit=False)
        male.contact1 = contact
        male.save()
        messages.success(request, f"Form Submitted: 
 {username}")
                    

Upvotes: 1

Related Questions