Warm tip: This article is reproduced from serverfault.com, please click

I want to store the id of one model in to another model -Django using Views

发布于 2020-11-28 11:53:26

Hi, I'm new in Django.

I'm working with web application where logged In user , can add a customer. the data have to store in two phase on phase is visit detail and in other phase all data will be saved through addcus

Visit_Detail model is the main Model and all the other Models have Foreign Key of Visit_Detail.

***** The Problem I'm facing is that how to get the value of Visit_detail.id and sore it in another view as a Foreign key.**

Here are my Models

UNIVERSAL/model

class Visit_Detail(models.Model):
   
    date_of_visit = models.DateTimeField(auto_now_add=True)
    reason_of_visit = models.CharField(max_length=200)
    number_of_visitors = models.IntegerField()
    visitors_added_by = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.reason_of_visit


class Customer_Status_Detail(models.Model):
    customer_visit_id = models.ForeignKey(Visit_Detail, on_delete=models.CASCADE)
    customer_follow_up_by = models.ForeignKey(User, on_delete=models.CASCADE)
    customer_pipeline_status = models.CharField(max_length=50)
    customer_decision_time = models.CharField(max_length=50)
    customer_follow_up_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.customer_pipeline_status

CUSTOMER/model

class Customer_Presentation_Detail(models.Model):
    customer_visit_id = models.ForeignKey(UNIVERSAL.Visit_Detail, on_delete=models.CASCADE)
    customer_presented_follow_up_visitor_added_by = models.ForeignKey(User, on_delete=models.CASCADE)
    customer_presentation_at = models.CharField(max_length=30)
    customer_presentation_at_other = models.CharField(max_length=30, null=True)
    customer_material_shared = models.CharField(max_length=30)
    customer_material_shared_qty = models.CharField(max_length=30, null=True)
    customer_feedback = models.CharField(max_length=1000)
    customer_suggestions = models.CharField(max_length=1000)
    customer_concerns = models.CharField(max_length=1000)

    def __str__(self):
        return self.customer_name


class Customer_Agent_Detail(models.Model):
    customer_visit_id = models.ForeignKey(UNIVERSAL.Visit_Detail, on_delete=models.CASCADE)
    customer_agent_business_address = models.CharField(max_length=500)
    customer_agent_works_in = models.CharField(max_length=500)
    customer_agent_area = models.CharField(max_length=500)
    customer_agent_customer_in = models.CharField(max_length=500)
    customer_agent_office = models.CharField(max_length=500)

    def __str__(self):
        return self.customer_agent_business_address


class Customer_Buyer_Detail(models.Model):
    customer_visit_id = models.ForeignKey(UNIVERSAL.Visit_Detail, on_delete=models.CASCADE)
    customer_buyer_investment_purpose = models.CharField(max_length=50, )
    customer_buyer_deal_size = models.CharField(max_length=50, )
    customer_buyer_deal_size_qty = models.CharField(max_length=50, )
    customer_buyer_requested_floor = models.CharField(max_length=50, )

    def __str__(self):
        return self.customer_buyer_investment_purpose


class Customer_Personal_Detail(models.Model):
    customer_visit_id = models.ForeignKey(UNIVERSAL.Visit_Detail, on_delete=models.CASCADE)
    customer_name = models.CharField(max_length=50)
    customer_age = models.IntegerField()
    customer_phn_number = models.CharField(max_length=14)
    customer_reference = models.CharField(max_length=50)
    customer_is_customer_of = models.CharField(max_length=50)
    customer_email = models.EmailField()
    customer_profession = models.CharField(max_length=50)
    customer_job_title = models.CharField(max_length=50)
    customer_address = models.CharField(max_length=50)

    def __str__(self):
        return self.customer_name

Here are my Models

UNIVERSAL/Views

def visitdetail(request):
    if request.session.has_key('is_logged'):
        if request.method == 'POST':
            # To save all fields in db

            visit_detail = Visit_Detail()

            # visit detail model saving
            visit_detail.reason_of_visit = request.POST.get('customer_visit_reason')
            visit_detail.number_of_visitors = request.POST.get('customer_quantity')
            visit_detail.visitors_added_by = request.user
            if visit_detail.reason_of_visit == "":
                messages.error(request, 'All Fields are Required')
                return redirect('../CUSTOMER/visitdetail', {})
            else:
                visit_detail.save()

                if visit_detail is None:
                    return render(request, 'visit_detail.html')
                else:
                    # return render(request, 'add_customer.html')
                    return render(request, 'staff_landing.html')
        else:
            return render(request, 'visit_detail.html')
    else:
        return render(request, 'login_staff.html')

CUSTOMER/views

def addcus(request):
if request.session.has_key('is_logged'):
    if request.method == 'POST':
        # To save all fields in db
        customer_presentation_detail = Customer_Presentation_Detail()
        customer_personal_detail = Customer_Personal_Detail()
        customer_status_detail = Customer_Status_Detail()
        customer_buyer_detail = Customer_Buyer_Detail()
        customer_agent_detail = Customer_Agent_Detail()
        # presentation model saving
        customer_presentation_detail.customer_visit_id = Visit_Detail.id

        customer_presentation_detail.customer_presented_follow_up_visitor_added_by = request.user
        customer_presentation_detail.customer_presentation_at = request.POST.get('customer_presentation_at')
        customer_presentation_detail.customer_presentation_at_other = request.POST.get('customer_presentation_at'
                                                                                       '_other')
        customer_presentation_detail.customer_material_shared = request.POST.get('customer_material_shared')
        customer_presentation_detail.customer_material_shared_qty = request.POST.get('customer_material_quantity')
        customer_presentation_detail.customer_feedback = request.POST.get('customer_feedback')
        customer_presentation_detail.customer_suggestions = request.POST.get('customer_suggestions')
        customer_presentation_detail.customer_concerns = request.POST.get('customer_concerns')

        # personal detail model saving
        customer_personal_detail.customer_name = request.POST.get('customer_name')
        customer_personal_detail.customer_age = request.POST.get('customer_age')
        customer_personal_detail.customer_phn_number = request.POST.get('customer_contact_no')
        customer_personal_detail.customer_reference = request.POST.get('customer_reference')
        customer_personal_detail.customer_is_customer_of = request.POST.get('customer_of')
        customer_personal_detail.customer_email = request.POST.get('customer_email')
        customer_personal_detail.customer_profession = request.POST.get('customer_profession')
        customer_personal_detail.customer_job_title = request.POST.get('customer_job_title')
        customer_personal_detail.customer_address = request.POST.get('customer_address')
        # customer status detail model saving
        customer_status_detail.customer_pipeline_status = request.POST.get('customer_pipeline_status')
        customer_status_detail.customer_decision_time = request.POST.get('customer_decision_time')
        customer_status_detail.customer_follow_up_date = request.POST.get('customer_followup_date')
        customer_status_detail.customer_follow_up_by = request.user
        # buyer detail model saving
        customer_buyer_detail.customer_buyer_investment_purpose = request.POST.get('customer_investment_detail')
        customer_buyer_detail.customer_buyer_deal_size = request.POST.get('customer_deal_size')
        customer_buyer_detail.customer_buyer_deal_size_qty = request.POST.get('customer_deal_size_qty')
        customer_buyer_detail.customer_buyer_requested_floor = request.POST.get('customer_required_floor')
        # agent detail model saving
        customer_agent_detail.customer_agent_office = request.POST.get('agent_office')
        customer_agent_detail.customer_agent_business_address = request.POST.get('agent_business_address')
        customer_agent_detail.customer_agent_works_in = request.POST.get('agent_works_in')
        customer_agent_detail.customer_agent_area = request.POST.get('agent_area')
        customer_agent_detail.customer_agent_customer_in = request.POST.get('agent_customer_in')

        if customer_personal_detail.customer_name == "":
            messages.error(request, 'All Fields are Required')
            return redirect('../CUSTOMER/addcus', {})
        else:
            customer_presentation_detail.save()
            customer_personal_detail.save()
            customer_status_detail.save()
            customer_buyer_detail.save()
            customer_agent_detail.save()

            if customer_personal_detail is None:
                return render(request, 'staff_landing.html')
            else:
                # return render(request, 'add_customer.html')
                return render(request, 'staff_landing.html')
    else:
        return render(request, 'staff_landing.html')
else:
    return render(request, 'login_staff.html')
Questioner
Sohaib Haneef
Viewed
0
Sohaib Haneef 2020-11-30 16:55:57

I just Changed the Visit_Detail View with This

class Visit_Detail(models.Model):

date_of_visit = models.DateTimeField(auto_now_add=True)
reason_of_visit = models.CharField(max_length=200)
number_of_visitors = models.IntegerField()
visitors_added_by = models.ForeignKey(User, on_delete=models.CASCADE)

def __int__(self):
    return self.id

and in view i just did this

'c_visit_id = Visit_Detail.objects.latest('id')
customer_presentation_detail.customer_presentation_visit_id = c_visit_id`

and it worked