Home > Enterprise >  Related Field got invalid lookup: contains
Related Field got invalid lookup: contains

Time:11-26

I am trying to include a search field inside my home page. It works for some of the module field. My problem is when I use a ForeignKey field (correct me please if I am wrong).

models.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)

views.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 code

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

please help me to solve this

My problem is if I use assign_to_trainer inside the filter query instead of Training_Lead I receive the error:

enter image description here

CodePudding user response:

You can't use __contains lookup in ForeignKey since it is used on strings so the Queryset should be:

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

CodePudding user response:

Hi you can do it this way in your filter query:

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),
        )
  • Here, assign_to_trainer_id expects an integer only.
  • Related