django 相关字段得到无效的查找:包含

jk9hmnmh  于 2022-11-26  发布在  Go
关注(0)|答案(2)|浏览(152)

我试图在我的主页中包含一个搜索字段。它对一些模块字段有效。我的问题是当我使用外键字段时(如果我错了请纠正我)。

型号.py

class Training_Lead(models.Model):
handel_by = models.ForeignKey(UserInstance, on_delete=models.PROTECT)
learning_partner = models.ForeignKey(
    Learning_Partner, on_delete=models.PROTECT, blank=False, null=False)
assign_to_trainer = models.ForeignKey(
    Trainer, on_delete=models.PROTECT, null=True, blank=True)
course_name = models.CharField(max_length=2000)
lead_type = models.CharField(max_length=2000)
time_zone = models.CharField(choices=(('IST', 'IST'), ('GMT', 'GMT'), ('BST', 'BST'), (
    'CET', 'CET'), ('SAST', 'SAST'), ('EST', 'EST'), ('PST', 'PST'), ('MST', 'MST'), ('UTC', 'UTC')), max_length=40, blank=False, null=False)
getting_lead_date = models.DateTimeField(null=True, blank=True)
start_date = models.DateTimeField(null=True, blank=True)
end_date = models.DateTimeField(null=True, blank=True)
lead_status = models.CharField(choices=(('Initial', 'Initial'), ('In Progress', 'In Progress'), ('Follow Up', 'Follow Up'), (
    'Cancelled', 'Cancelled'), ('Confirmed', 'Confirmed'), ('PO Received', 'PO Received')), max_length=40, blank=False, null=False)
lead_description = models.CharField(max_length=9000, blank=True, null=True)

def __str__(self):
    return str(self.assign_to_trainer)

class Meta:
    ordering = ['start_date']



class Trainer(models.Model):
    trainer_id = models.AutoField(primary_key=True)
    trainer_name = models.CharField(max_length=200, null=False, blank=False)
    address = models.CharField(max_length=500, null=False, blank=False)
    phone_no = models.CharField(max_length=13, unique=True, null=True, blank=True)
    phone_no_optional = models.CharField(max_length=13, null=True, blank=True)
    email = models.CharField(max_length=50)
    email_optional = models.CharField(max_length=50, null=True, blank=True)
    country = models.CharField(max_length=50, null=True, blank=True)
    primary_language = models.CharField(max_length=50, null=True, blank=True)
    gender = models.CharField(choices=(('Male', 'Male'), ('Female', 'Female')), max_length=30, blank=True, null=True)
    trainer_type = models.CharField(choices=(('Corporate Trainer', 'Corporate Trainer'), ('Academic Trainer', 'Academic Trainer')), max_length=30, blank=True, null=True)
    trainer_pricing = models.CharField(max_length=1000, null=True, blank=True)
    trainer_course_specialization = models.CharField(max_length=5000)
    trainer_skill_set = models.CharField(max_length=10000, null=True, blank=True)
    trainer_enrolled_with = models.ForeignKey(Learning_Partner, on_delete=models.PROTECT, blank=True, null=True)
    trainer_tier = models.CharField(choices=(('1', '1'), ('2', '2'), ('3', '3'), ('4', '4')), max_length=10, null=True, blank=True)


    def __str__(self):
        return str(self.trainer_name)

查看次数.py

def report_for_trainer(request):
if request.user.is_authenticated:
    user = UserInstance.objects.all()
    partner = Learning_Partner.objects.all()
    trainer_info = Trainer.objects.all()
    trainer = Training_Lead.objects.all()
    if request.method == "POST":

        start_date = request.POST.get("start_date")
        end_date = request.POST.get("end_date")
        lead_status = request.POST.get("lead_status", default="")
        assign_to_trainer = request.POST.get("assign_to_trainer")
        trainers_info = Training_Lead.objects.filter(
            start_date__gte=start_date,
            end_date__lte=end_date,
            lead_status__contains=lead_status,
            assign_to_trainer_id__contains=assign_to_trainer,
        )
        trainer_info_not_active = Training_Lead.objects.exclude(
            start_date__gte=start_date,
            end_date__lte=end_date,
            lead_status__contains=lead_status,
        )
        
        df = {"user": user, "partner": partner,"start_date": start_date,"end_date":end_date,"trainer":trainer,"lead_status":lead_status,"assign_to_trainer":assign_to_trainer, "lead_status": lead_status,"trainers_info": trainers_info, "trainer_info": trainer_info, "trainer_info_not_active": trainer_info_not_active}
    return render(request, "trainers_for_schedule_date.html", df)
else:
    return redirect("router")

HTML程式码

<label class="form-label" for="assign_to_trainer">
          <h4 style="color: rgb(0, 0, 0);">Assign To Trainer :-</h4>
      </label>
      <select name="assign_to_trainer" id="assign_to_trainer" multiple>
        {% for t in leads %}
        <option name="assign_to_trainer" id="assign_to_trainer" value="{{ t.assign_to_trainer_id }}">{{ t.assign_to_trainer }}</option>
        {% endfor %}
      </select>

请帮我解决这个问题
我的问题是,如果我在筛选器查询中使用assign_to_trainer而不是Training_Lead,我会收到错误:
enter image description here

ws51t4hk

ws51t4hk1#

您不能在ForeignKey中使用__contains查找,因为它用于字符串,因此查询集应为:

trainers_info = Training_Lead.objects.filter(
            start_date__gte=start_date,
            end_date__lte=end_date,
            lead_status__contains=lead_status,
            assign_to_trainer=assign_to_trainer,
        )
ejk8hzay

ejk8hzay2#

嗨,您可以在筛选查询中这样做:

trainers_info = Training_Lead.objects.filter(
            start_date__gte=start_date,
            end_date__lte=end_date,
            lead_status__contains=lead_status,
            assign_to_trainer_id=int(assign_to_trainer),
        )
  • 此处,assign_to_trainer_id只需要一个整数。

相关问题