How to display detail objects after filtering their primary object in django?

I am working on an app that can create teams, Players,I am now on displaying player objects which are connected by a foreign key teams that displays relevant players to their team.But the problem I am getting is when the player objects are displayed in detail view,all the players get displayed while that specific player objects should be displayed.Please help me to solve the problem.

here,s the html

    {% include 'games_app/base.html' %}
    {% block body_block %}

<div>
    {% for players in play.teams.all %}
    <h1>Players :<strong>{{players.player_name}}</strong></h1>
    <h1>Players-age :<strong>{{players.player_age}}</strong></h1>
    <h1>Players-form :<strong>{{players.player_form}}</strong></h1>
    <h1>Players-over :<strong>{{players.over}}</strong></h1>
    <h1>Players-batting :<strong>{{players.batting}}</strong></h1>
    <a href="{% url 'games_app:players_update' players.pk %}">Edit Player</a></br>
    <a href="{% url 'games_app:players_delete' players.pk %}">Delete Player</a>
    {% endfor %}
</div>

    {% endblock %}

Here,s the views.py of detail players

def Detail_Players(request,pk):

    model = models.Team.objects.get(pk=True)

    template_name = 'games_app/players_detail.html'


    return render(request, template_name, context=model)

[SOLVED] I just changed the model and other details.

You should try this:

model = models.Team.objects.get(pk=pk)

And then pass the context to the template in the correct way.

Making queries | Django documentation, See the documentation for save() for complete details. In SQL terms, a QuerySet equates to a SELECT statement, and a filter is a For example, this returns the first Entry in the database, after ordering entries alphabetically by headline: they apply to any object linked to the primary model, not necessarily those objects� Django provides extra-ordinary support for Detail Views but let’s check how it is done manually through a function-based view. This article revolves around Detail View which involves concepts such as Django Forms, Django Models. For Detail View, we need a project with some models and multiple instances which will be displayed.

Looks like you are setting pk to True instead of pk.

model = models.Team.objects.get(pk=True)

should be

model = models.Team.objects.get(pk=pk)

Built-in class-based generic views | Django documentation, Display list and detail pages for a single object. Present date-based objects in year/month/day archive pages, associated detail, and “latest” pages. However if any class attempts to override a key after parent classes have set it (after the call DetailView uses to find the value of the primary key used to filter the queryset. ListView ¶ class django.views.generic.list.ListView¶. A page representing a list of objects. While this view is executing, self.object_list will contain the list of objects (usually, but not necessarily a queryset) that the view is operating upon.

in views.py there is simplest way to create your detailview as following:

from django.views import generic
class DetailView(generic.DetailView):
    model = Team
    template_name = 'games_app/players_detail.html'

then in urls.py Pk should be passed as avariable to determine to which player you want to go:

path('<pk>/detail', views.DetailView.as_view(), name='detail'),

then in your template use method "set" through your for loop as follows:

 {% for player in team.player_set.all %}
....

QuerySet API reference | Django documentation, Internally, a QuerySet can be constructed, filtered, sliced, and generally passed See the following section for details of what is involved when pickling QuerySets. an object, with the keys corresponding to the attribute names of model objects. are executed after the QuerySet has begun to be evaluated and the primary� We can recognize certain common tasks, like displaying a list of objects, and write code that displays a list of any object. Then the model in question can be passed as an extra argument to the URLconf. Django ships with generic views to do the following: Display list and detail pages for a single object.

Model instance reference | Django documentation, This document describes the details of the Model API. MyModel.objects.filter(pk =obj.pk).update(val=F('val') + 1) # At this point obj.val aren't present on your form from being validated since any errors raised could not be corrected by the user. Specifically, when you call save() and the object's primary key attribute does� If the object’s primary key attribute is not set or if the UPDATE didn’t update anything (e.g. if primary key is set to a value that doesn’t exist in the database), Django executes an INSERT. If the object’s primary key attribute defines a default then Django executes an UPDATE if it is an existing model instance and primary key is set

Query Expressions | Django documentation, Company.objects.filter(num_employees__gt=F('num_chairs')) # Find This persistence can be avoided by reloading the model object after saving it, for When referencing relational fields such as ForeignKey , F() returns the primary key value rather than a model instance: > For more details see Conditional Expressions. Successive filter() calls further restrict the set of objects, but for multi-valued relations, they apply to any object linked to the primary model, not necessarily those objects that were selected by an earlier filter() call. That may sound a bit confusing, so hopefully an example will clarify.

Accessing Related Data Using Django ORM, The type of the objects we handled till now were simple objects that can Let's try creating a Category object and see what happens: object wile creating Category object, you could also pass primary key of the Once a Post object is saved into the database, we can add tags to it later as you will see. A Python “magic method” that returns a string representation of any object. This is what Python and Django will use whenever a model instance needs to be coerced and displayed as a plain string. Most notably, this happens when you display an object in an interactive console or in the admin.

Comments
  • what do you mean by setting the context in a correct way
  • after doing what you said i get Team matching query does not exist.
  • So you want a pk that does not exist in your DB.
  • You want to use play.teams.all but does not declare in your view
  • It exists but i don't know why it gives me this error
  • i get an error then Team matching query does not exist.
  • I just solved the problem by setting the model to Players and then displaying the data , It was a foolish mistake yet so tricky. :)