我想使用Views将一个模型的id存储到另一个模型Django中



嗨,我是Django的新手

我正在使用web应用程序,登录用户可以添加客户。数据必须分两个阶段存储,一个阶段是访问细节,另一个阶段所有数据都将通过addcus保存

Visit_Detail模型是主模型,所有其他模型都有Visit_Details外键。

*****我面临的问题是如何获得Visit_detail.id的值,并将其作为外键在另一个视图中使用。**

这是我的型号

UNIVERSAL/型号

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

客户/型号

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

这是我的型号

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')

客户/查看

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')

我认为这对你不起作用,但一个想法

get_post_id = Post.objects.get(name='Jhon')
print(get_post_id.id)

下面是我的建议列表:

  1. 您可以使用@login_required装饰器而不是request.session.has_key('is_logged'(
  2. 你的观点很难理解。尝试使用表单
  3. 要首先获取id,必须获取对象obj = Visit_Detail.objects.get(id=pk),然后才能获取c = Customer_Status_Detail()c.customer_visit_id = obj

我刚刚用这个更改了Visit_Detail视图

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

在视图中,我刚刚做了这个

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

并且它工作

相关内容

  • 没有找到相关文章

最新更新