As you can see in a picture I have Staff Worker named Max Barren but the thing is he is connected only to one of these shops. How do I write code properly for this staff worker only under one of these shops?
views.py
def list_view(request):
context = {}
context['library_dataset'] = VideoLibrary.objects.all()
context['staff_dataset'] = Staff.objects.all()
context['rank_dataset'] = Rank.objects.all()
return render(request, 'app/list_view.html', context)
at views.py I get objects like address and shop name from the VideoLibrary model and staff_name and gender from the Staff model to put it in list_view.html
models.py
class VideoLibrary(models.Model):
shop_name = models.CharField(max_length=264)
adress = models.TextField(max_length=264, default='')
def __str__(self):
return self.shop_name
class Meta:
verbose_name_plural = "Video Libraries"
class Staff(models.Model):
staff_name = models.CharField(max_length=264)
living_adress = models.TextField(max_length=264)
gender = models.CharField(max_length=264)
age = models.IntegerField()
staff_rating = models.IntegerField()
shop_name = models.ForeignKey(VideoLibrary, on_delete=models.CASCADE)
def __str__(self):
return f'{self.staff_name}'
class Meta:
verbose_name_plural = "Staff"
class Rank(models.Model):
# rank_id = models.AutoField(primary_key=True, unique=True)
staff_name = models.ForeignKey(Staff, on_delete=models.CASCADE)
rank_name = models.CharField(max_length=264)
def __str__(self):
return f'{self.rank_name}, {self.staff_name}'
class Meta:
verbose_name_plural = "Ranks"
I suppose I got some trouble with my code at list_view.html but I don't know how to figure it out
list_view.html
<div >
{% for data in library_dataset %} Shop name:
<br />
<br />
{{ data.shop_name }} <br />
{{ data.adress }} <br />
<td><a href="{% url 'update_shop' data.id %}">
<button type="button">Edit Shop Details</button>
</a></td>
<br />
Staff that work in the shop:
{% for data in staff_dataset %} Shop name:
<br />
<br />
{{ data.staff_name }} <br />
{{ data.gender }} <br />
<td><a href="{% url 'update_staff' data.id %}">
<button type="button">Edit User Details</button>
</a></td>
<br />
<br />
------------------------------------------------------------------------------------------------------------
<br>
{% endfor %}
{% endfor %}
CodePudding user response:
You are passing to context all VideoLibrary objects and all Staff objects. So when you are doing
{% for data in library_dataset %}
Displaying data of all shops
{% for data in staff_dataset %}
Displaying data of all staff
{% endfor %}
{% endfor %}
You display every user in database for every shop in your database. You should use related name of staff from your VideoLibrary object. More on that here: https://docs.djangoproject.com/en/4.0/topics/db/queries/#backwards-related-objects In your case it would be probably .staff_set.all() to get all staff objects related to shop, so your html code would be:
{% for data in library_dataset %}
Displaying data of all shops
{% for staff in data.staff_set.all %}
Displaying data of staff related to libary
{% endfor %}
{% endfor %}