user3604978
user3604978

Reputation: 11

Using one model to filter another model in Django

I'm trying to access the information in my gadb_action model based on the action_ids in my gadb_vote model. I'm initially only getting the information for a particular legislator and then attempting to get the bills associated with the actions that legislator has voted on.

Right now, my action_list is only storing action_ids, but not the related information from the gadb_action model that I want to use in my template.

What is the best way to store that information outside of the for loop to be accessed by the template? Is there a way to write to an empty QuerySet?

Thanks in advance for any and all help!

view

def each_member(request,legislator_id):
    each_member = get_object_or_404(gadb_legislator, legislator_id=legislator_id)
    each_vote = gadb_vote.objects.filter(legislator_id=legislator_id)
    action_list = []

    for i in each_vote:
        action = gadb_action.objects.filter(action_id=i.action_id)
        action_list.append(action)

    context = {
        'each_member': each_member,
        'each_vote': each_vote,
        'action_list': action_list
    }

    return render(request, "eachmember.html", context)

models

class gadb_action(models.Model):
    action_id = models.IntegerField(unique=False, max_length=4, primary_key=True)
    bill_id = models.IntegerField(unique=False, max_length=12)

class gadb_vote(models.Model):
    vote_id = models.IntegerField(unique=False, max_length=11,primary_key=True)
    legislator_id = models.IntegerField(unique=False, max_length=11)
    action_id = models.IntegerField(unique=False, max_length=11)

template

{% for i in action_list %}
    {{i.bill_id}}
    {{i.action_id}}
{% endfor %}

Upvotes: 1

Views: 738

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599480

Your models are broken.

For a start, although it's not directly related to the question, you need to define your primary keys as AutoFields so that they are autoincremented every time a new entity is added. Otherwise you'll get all sorts of errors when you save a new row. (Even better, don't define the PK at all, and let Django add it automatically.)

Secondly, as lalo says, you should have ForeignKeys from Action to Bill, and from Vote to Action and Vote to Legislator. That way you can get the relevant information with a single query, and follow the foreign keys as required in your template.

(Also, Django already includes the app name in the underlying table name: no need to prefix everything with 'gadb'.)

class Action(models.Model):
    bill = models.ForeignKey(Bill)

class Vote(models.Model):
    legislator = models.ForeignKey(Legislator)
    action = models.ForeignKey(Action)

View:

def each_member(request,legislator_id):
    actions = Action.objects.filter(vote__legislator_id=legislator_id)
    return render(request, "eachmember.html", {'action_list': actions})

Template:

{% for action in actions %}
    {{ action.bill.name }}
    {{ action.someotherfield }}
{% endfor %}

Upvotes: 4

Related Questions