How to check if an identical element exists in another table in Django?

Question:

I have two models: CaseRequest and Case. The CaseRequest object has values name and datebirth. I need to check if the same values exists for name and datebirth in Case. For instance, The result should be like this:

Case:

Name DateBirth
Don Honn 01.03.1959
Freak Hu 04.11.1993
Wend Don 06.03.1988

CaseRequest:

Name DateBirth 
Tom Hawk 01.05.1999 - no
Don Honn 01.03.1959 - yes
Fold Len 03.07.1967 - no
Freak Hu 04.11.1993 - yes
Wend Don 13.12.1956 - no

My code:
Models.py

class CaseRequest(models.Model):
    name = models.CharField(max_length=255)
    datebirth = models.DateField()
    status = models.CharField(max_length=255)
    timeapplication = models.DateField()
    category = models.CharField(max_length=255)
    def __str__(self):
        return self.name
    def get_absolute_url(self):
        return reverse('caserequest')

class Case(models.Model):
    name = models.CharField(max_length=255)
    datebirth = models.DateField()
    def __str__(self):
        return self.name + ' | ' + str(self.datebirth)
    def get_absolute_url(self):
        return reverse('case_list')

Views.py

class CaseRequestView(ListView):
    model = CaseRequest
    template_name = 'caserequest.html'

caserequest.html

<div>
  {% for caserequest in object_list %}
        <div>
            <p>{{ caserequest.name }}, {{ caserequest.datebirth }}</p>
            <!-- The result should be here: YES or NO -->
        </div>
        {% endfor %}
</div>
Asked By: Drozon

||

Answers:

I think you must to create a py file called utils.py or similar in same directory where you have view.py with following code:

from django import template
from ptii.models import Case

register = template.Library()


@register.simple_tag
def get_case(caserequest_name, caserequest_datebirth):
    if Case.objects.filter(name=caserequest_name).filter(
            datebirth=caserequest_datebirth).exists():
        return('Yes')
    else:
        return('No')

and in your html you have to load the file utils.py and use the function like this:

{% load utils.py %} {# load the file #}

{% get_case caserequest.name caserequest_datebirth as exist %} {# get Yes or No #}

Then you can use exist field in yout html as this:

{{ exist }}

Answered By: Afj

You can simply create a @property decorator, say for example case_exists in the models so:

class CaseRequest(models.Model):
    name = models.CharField(max_length=255)
    datebirth = models.DateField()
    status = models.CharField(max_length=255)
    timeapplication = models.DateField()
    category = models.CharField(max_length=255)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('caserequest')
    
    @property
    def case_exists(self):
        return Case.objects.filter(name=self.name, datebirth=self.datebirth).exists()

Then in the template use it as:


<div>
    {% for caserequest in object_list %}
        <div>
            <p>{{ caserequest.name }}, {{ caserequest.datebirth }} - {% if caserequest.case_exists %} Yes {% else %} No {% endif %}</p>
        </div>
    {% endfor %}
</div>
Answered By: Sunderam Dubey

please read below information and use "pool_extras" instead of "utils" to have a complete vision.

As you can see you must to create a directory called "templatetags" and inside it put you file, dont forget to create the file init.py


How to create custom template tags and filters¶

Django’s template language comes with a wide variety of built-in tags and filters designed to address the presentation logic needs of your application. Nevertheless, you may find yourself needing functionality that is not covered by the core set of template primitives. You can extend the template engine by defining custom tags and filters using Python, and then make them available to your templates using the {% load %} tag.

The most common place to specify custom template tags and filters is inside a Django app. If they relate to an existing app, it makes sense to bundle them there; otherwise, they can be added to a new app. When a Django app is added to INSTALLED_APPS, any tags it defines in the conventional location described below are automatically made available to load within templates.

The app should contain a templatetags directory, at the same level as models.py, views.py, etc. If this doesn’t already exist, create it – don’t forget the init.py file to ensure the directory is treated as a Python package.

Development server won’t automatically restart

After adding the templatetags module, you will need to restart your server before you can use the tags or filters in templates.

Your custom tags and filters will live in a module inside the templatetags directory. The name of the module file is the name you’ll use to load the tags later, so be careful to pick a name that won’t clash with custom tags and filters in another app.

For example, if your custom tags/filters are in a file called poll_extras.py, your app layout might look like this:

polls/
init.py
models.py
templatetags/
init.py
poll_extras.py
views.py

And in your template you would use the following:

{% load poll_extras %}

Answered By: Afj