【问题标题】:I want to store the id of one model in to another model -Django using Views我想将一个模型的 id 存储到另一个模型中-Django using Views
【发布时间】:2020-11-28 11:53:26
【问题描述】:

大家好,我是 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')

【问题讨论】:

    标签: django django-models foreign-keys


    【解决方案1】:

    我不认为它对你有用,而是一个想法

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

    【讨论】:

    • 它有助于解决问题,在打印系统中没有返回 Id 而是返回其他内容,所以我知道问题出在模型上.... this def __int__(self) : 返回 self.id
    【解决方案2】:

    下面是我的建议列表:

    1. 您可以使用 @login_required 装饰器代替 request.session.has_key('is_logged')
    2. 您的观点非常难以理解。尝试使用forms
    3. 要先获取id,你必须先获取对象obj = Visit_Detail.objects.get(id=pk),然后你可以c = Customer_Status_Detail()c.customer_visit_id = obj

    【讨论】:

      【解决方案3】:

      我刚刚用这个更改了 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`
      

      而且成功了

      【讨论】:

        猜你喜欢
        • 2015-02-27
        • 2021-03-26
        • 1970-01-01
        • 1970-01-01
        • 2020-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-25
        相关资源
        最近更新 更多