If you need to edit the race and the candidates on the same page, and
the candidates are FK'ed to the race, I'd do it like this:
(Warning, just coding off the top of my head, might have bad syntax/
imports)
forms.py
-----------------
from django import forms
from django.forms.models import modelformset_factory
from models import Candidate, Race
class RaceForm(forms.ModelForm):
class Meta:
model = Race
CandidateFormSet = modelformset_factory(Candidate, extra=0)
views.py
------------------
from django.shortcuts import get_object_or_404, redirect
from django.views.generic.simple import direct_to_template
from forms import RaceForm, CandidateFormSet
from models import Candidate, Race
def edit_race(request, race_slug):
race = get_object_or_404(slug=race_slug)
race_form = RaceForm(request.POST or None, instance=race)
candidate_formset = (request.POST or None,
queryset=Candidate.objects.filter(race=race))
if race_form.is_valid() and candidate_formset.is_valid():
race_form.save()
candidate_formset.save()
return redirect('.') #Or whatever page you want them to go to
after saving
return direct_to_template(request, 'edit_race.html', {
'race_form': race_form,
'candidate_formset': candidate_formset,
})
edit_race.html
---------------------
{% block content %}
<form action="." method="post">
{{ race_form }}
{{ candidate_formset }}
<button type="submit">Save</button>
</form>
{% endblock %}
Hope I understood the question and that this helps,
Alex
On Jul 23, 8:23 am, Nick <[email protected]> wrote:
> I have inherited a project with a tight deadline and I am lost for how
> to proceed.
>
> There are two tables: Canididates and Race.
>
> The race table holds 145 different races with information about
> precincts, winners of different stages of the race (primary, runoff,
> gneral)
>
> The Candidates table has 1100+ candidates, each one is assigned to a
> specific race via a FK and a list of three race vote totals (general,
> runoff, primary)
>
> I am supposed to create a way to display each race based on a search
> and then return the candidates associated with that race so that the
> race vote totals can be updated. I will also need to be able to update
> values in the Race table.
>
> It would be highly preferable for this to be seemless for the data
> input people. I am looking at producing something like this:
>
> Race 1
> precincts reporting input - precincts total (static value)
> Candidate 1 primary vote input - Candidate 1 status (active,
> eliminated)
> Candidate 2 primary vote input - Candidate 2 status (active,
> eliminated)
>
> winner of primary (list of candidates)
>
> I have most of the form fields and widgets ready to go, but being able
> to edit multiple entries in two different tables seemless has me
> stuck. Is this something that I should handle in inlineformsets since
> I am spanning an FK? Would I need modelformsets to display all of the
> candidates?
>
> I can easily truncate the Race table and change it so that it has a
> many to many relationship to all of the candidates instead of the
> Candidates having an FK relationship to the race? Would that give me
> more flexibility?
>
> Thanks for the help in figuring this out, this has been an incredibly
> stressful project.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.