django - 我想将一个模型的 id 存储到另一个模型中-Django using Views
问题描述
嗨,我是 Django 的新手。
我正在使用登录用户的 Web 应用程序,可以添加客户。数据必须存储在两个阶段,阶段是访问详细信息,在其他阶段,所有数据都将通过addcus保存
Visit_Detail 模型是主模型,所有其他模型都有 Visit_Detail 的外键。
***** 我面临的问题是如何获取 Visit_detail.id 的值并在另一个视图中将其作为外键。**
这是我的模型
通用/型号
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
这是我的模型
通用/视图
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)
推荐阅读
- javascript - 如何遍历选定元素并将样式应用于每个元素?
- ruby - 如何检查 selenium 是否在 Ruby 上的浏览器上工作
- oracle - 为什么我运行脚本时输出为空白
- ios - 如何在不覆盖 UITabBarItem.appearance() 的情况下设置 UITabBarAppearance()?
- python - 如何在不使用 Driver.get 的情况下使用 Selenium 单击元素?
- apache-spark - 你如何在 pyspark 中为 RDD 过滤一个 EXACT 词?
- java - NetBeans 历史记录未显示
- php - PHP - 根据用户回答形成问题群体
- javascript - 模块构建失败(来自 ./node_modules/@nuxt/webpack/node_modules/babel-loader/lib/index.js):
- python - 尽管安装了 pygame,但无法将其导入 VScode