1

I built a website with Python Django and part of it displays lines of already input data with the primary key next to it. Now I have separate user data by using a foreign key field in each one of my models and the specific user's data is only shown to that logged in user except that all the data regardless of the user is saved in one spot with the model shown in the admin interface(because I'm using foreign keys to separate data). My problem is that I need to display the primary key of just the logged-in user. Take this, for example, if User1 adds 1 line of data to their page then User2 adds 1 line of data on their page which will appear separate from one another, then User1 uploads another line of data, Then I user traditional primary key to number the data lines of User1 the numbers will be 1 and three instead of keeping them in numerical order of 1 and 2 and disregarding User2's data in the counting of the data. It's as if I need a separate primary key for each user. Sorry this is really hard to explain. I have found a temporary solution of using {{ forloop.revcounter }} to count in a for loop instead of using the primary key but the problem with this is that when data is deleted all the numbers above it go down one because this tag just counts how many times the for loop has looped. I have found no information of the internet about this and how to solve it beyond this. I might just be looking in the wrong places but I need help. Thanks!

EDIT:

models.py

class Sheet_Extinguisher(models.Model):
    user = models.ForeignKey(User, default=True, related_name="Extinguisher", on_delete=models.PROTECT)
    floor = models.CharField(max_length=5, choices=FLOOR_LEVEL_FE, blank=True, null=True, verbose_name='Floor / Level')
    area_room = models.CharField(max_length=35, blank=True, null=True, verbose_name='Area / Room')
    building_address = models.CharField(max_length=46, choices=BUILDING_ADDRESS, blank=True, null=True, verbose_name='Building and Address')
    type = MultiSelectField(choices=TYPE_FE, blank=True, null=True, verbose_name='Type')
    size = models.CharField(max_length=3, choices=SIZE_FE, blank=True, null=True, verbose_name='Size')
    hydrostatic_date = models.DateField(blank=True, null=True, verbose_name='Last Hydrostatic Test')
    mass_notes = models.CharField(max_length=20, blank=True, null=True, verbose_name='Mass Fire Tech Notes')
    notes = models.TextField(max_length=500, blank=True, null=True, verbose_name="Inspector's note")

    class Meta:
        ordering = ['building_address']

    def __str__(self):
        return self.building_address or 'None'

    def get_absolute_url(self):
        return reverse('list_extinguisher')

HTML (with pk):

{% if filter.qs %}
    {% for post in filter.qs %}
        <tr>
        <td><div class="item1" style="text-align: center;"><a href="{% url 'delete_building' pk=post.pk %}">{{ post.pk }}</a></div></td>
    {% endfor %}
{% endif %}

HTML (just with forloop counter):

{% if filter.qs %}
    {% for post in filter.qs %}
        <tr>
        <td><div class="item1" style="text-align: center;"><a href="{% url 'delete_building' pk=post.pk %}">{{ forloop.revcounter }}</a></div></td>
    {% endfor %}
{% endif %}
Russell Hertel
  • 121
  • 3
  • 14

0 Answers0