How to show and keep the last record in Django

Question:

I’m new to Django I hope I’m making sense. I have two models: Usecase and Usecase_progress. I’m creating a page where I can see a list of all the use cases with their information + the usecase progress for each use case gets recorded.

Usecase model:

class Usecase(models.Model):
usecase_id = models.CharField(primary_key=True, max_length=20)
usecase_name = models.CharField(max_length=256)
user_email = models.ForeignKey('User', models.DO_NOTHING, db_column='user_email')
usecase_type = models.ForeignKey('UsecaseType', models.DO_NOTHING)
kpi = models.ForeignKey(Kpi, models.DO_NOTHING)
usecase_git = models.CharField(max_length=512, blank=True, null=True)
delivery_date = models.DateField()
class Meta:
    managed = False
    db_table = 'usecase'

Usecase_progress model:

class UsecaseProgress(models.Model):
usecase_progress_date = models.DateTimeField(primary_key=True)
usecase_progress_value = models.IntegerField()
estimated_date_delivery = models.DateField(db_column='Estimated_Date_Delivery') 

My views.py:

def view_usecase(request):
     usecase_details = Usecase.objects.all()
     usecase_details = usecase_details.filter().prefetch_related("usecaseids")
     context = {'usecase_details': usecase_details}
     return render(request, 'ViewUsecase.html', context)

My template:

{% extends 'EmpDashboard.html' %}
{% block body %}

<div class="row d-flex">
    <div class="col-12 mb-4">
        <div class="card border-light shadow-sm components-section d-flex">
            <div class="card-body d-flex row col-12">
                <div class="row mb-4">
                    <div class="col-lg-12 col-sm-16">
                        <h3 class="h3 mb-4">View Usecases:</h3>
                    </div>
                    {% if usecase_details is not none and usecase_details %}
                    <div class="table-responsive">
                        <table id="example" class="table table-flush text-wrap table-sm" cellspacing="0" width="100%">
                            <thead class="thead-light">
                                <tr>
                                    <th scope="col">No.</th>
                                    <th scope="col">Usecase ID</th>
                                    <th scope="col">Usecase Name</th>
                                    <th scope="col">Client</th>
                                    <th scope="col">KPI</th>
                                    <th scope="col">Progress</th>
                                    <th scope="col">Progress date</th>
                                    <th scope="col">Pipeline</th>
                                    <th scope="col">View</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for result in usecase_details %}
                                <tr>
                                    <td>{{ forloop.counter }}</td>
                                    <td><span class="badge bg-info">{{result.usecase_id}}</span></td>
                                    <td>{{result.usecase_name}}</td>
                                    <td>{{result.business_owner.business_owner_name}}</td>
                                    <td>{{result.kpi.kpi_name}}</td>
                                    {% for progress in result.usecaseids.all %}
                                    <td><div class="progress-wrapper">
                                        <div class="progress-info">
                                        <div class="progress-percentage">
                                        <span>{{progress.usecase_progress_value}}</span>
                                        </div>
                                        </div>
                                        <div class="progress">
                                        <div class="progress-bar bg-success" role="progressbar" style="width: 60%;" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100"></div>
                                        </div>
                                        </div>
                                    </td>
                                    <td>{{progress.usecase_progress_date}}</td>
                                    <td>{{progress.pipeline.pipeline_name}}</td>
                                    {% endfor %}
                                    <td>
                                        <a href="/view-usecase/{{result.usecase_id}}" class="btn btn-success">VIEW</a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="col-lg-12 col-sm-16">
                      
                        <h3 class="h3 text-center">
                            No Records Found!
                        </h3>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>  
{% endblock %}

My question is how do I get the last record and keep the last record even if new record is updated. because what I’m getting the list of all the records.

Asked By: JHS99

||

Answers:

in models use

created = models.DateField(auto_now_add=True)
updated = models.DateField(auto_now=True)

and in views use

usecase_details = usecase_details.filter().prefetch_related("usecaseids").order_by('-created')
or 
usecase_details = usecase_details.filter().prefetch_related("usecaseids").order_by('-updated')

you can use the order_by() method to sort the records in reverse chronological order and the limit() method to limit the number of records to be displayed.

Answered By: Abdullah Al Harun