Django表格没有得到正确的列

时间:2018-05-23 01:16:13

标签: django

我已经挣扎了大约一个小时,似乎无法找到解决方案。

我有一个django模型,我使用ModelForm创建了表单。表单在视图中,我想在提交到数据库之前操作表单变量。问题是我似乎无法从表单的数据库中获取正确的列。相反,它看起来像是引用另一个相关表中的列。有什么建议吗?

模型

class RoutinePlans(models.Model):
    routine = models.ForeignKey(Routines, on_delete='CASCADE')
    exercise = models.ForeignKey(WeightExercises, on_delete='PROTECT')
    set = models.IntegerField()
    set_type = models.ForeignKey(SetType, on_delete='PROTECT')
    reps = models.IntegerField()
    day = models.IntegerField()
    week = models.IntegerField()

    def __str__(self):
        return self.routine.name

class Routines(models.Model):
    name = models.CharField(max_length=50)
    level = models.ForeignKey(RoutineLevels, on_delete='PROTECT')
    creator = models.ForeignKey(User, on_delete='CASCADE')
    status = models.TextField(max_length=50)
    description = models.TextField(max_length=255, null='TRUE', default=None)

    def __str__(self):
        return self.name

表单

class PlanForm(forms.ModelForm):
    DAY_CHOICES = (('1', '1'), ('2', '2'), ('3', '3'), ('4', '4'), ('5', '5'), ('6', '6'), ('7', '7'))
    day = forms.ChoiceField(widget=forms.Select, choices=DAY_CHOICES)
    class Meta:
        model = RoutinePlans
        exclude = ['routine']

视图

#not sure if this view would have anything to do  with the error but figured I would include it to give the full perspective
def createplan(request):
    form = forms.CreatePlan()

    if request.method == 'POST':
        form = forms.CreatePlan(request.POST)

        if form.is_valid():
            obj = form.save(commit=False)
            obj.name = request.POST['name']
            obj.creator_id = request.user.id
            obj.status = "DRAFT"
            obj.save()
            return redirect('fitnessmanagement:editplan', id=obj.pk)
        else:
            print('error form invalid')
    variables = {'form': form}

    return render(request, template_name='createplan.html', context=variables)

def editplan(request, routine_id):

    form = forms.PlanForm()
    routine_name = Routines.objects.get(id=routine_id).name

    if request.method == 'POST':
        form = forms.PlanForm(data=request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            #this is where I want to put obj.routine but the form will only pull fields from the Routines model and not the Routine Plans
            obj.save()
            return redirect('fitnessmanagement:editplan', routine_id=routine_id)
    # variables to populate plan
    plan = RoutinePlans.objects.filter(routine_id=routine_id)
    plan_weeks = RoutinePlans.objects.filter(routine_id=routine_id).values('week').distinct()
    plan_dayss = RoutinePlans.objects.filter(routine_id=routine_id).values('day', 'week').distinct()
    plan_excercise_name = RoutinePlans.objects.filter(routine_id=routine_id).values('day', 'week', 'exercise_id', 'set_type_id').distinct()
    plan_excercise = RoutinePlans.objects.filter(routine_id=routine_id).prefetch_related('exercise')
    names = WeightExercises.objects.all()
    setDetails = RoutinePlans.objects.filter(routine_id=routine_id).values('set', 'reps', 'day', 'week', 'exercise', 'set_type')
    set_type = SetType.objects.all()
    # end variables to populate plan

    variables = {'form': form,
             'id': routine_id,
             'routine_name':routine_name,
             'plan': plan,
             'plan_weeks': plan_weeks,
             'plan_exercises': plan_excercise,
             'plan_exercise_name': plan_excercise_name,
             'plan_days': plan_dayss,
             'setDetails': setDetails,
             'names': names,
             'set_type': set_type,
             }

    return render(request, template_name='editplan.html',context=variables)

模板

{% extends 'layout/master-layout.html' %}
{% load static %}
{% block  content %}
<section id="content">
    <!--start container-->
    <div class="container">
        <div class="row">

            <div class="col s12 m6 l4">
                <div class="card-panel">
                    <h4 class="header2">Add Workout</h4>
                    <div class="row">
                        <form class="col s12" method="POST">
                            {% csrf_token %}
                            <div class="row">
                                <div class="input-field col s12">
                                    {{ form.week }}
                                    <label for="weekNumber">Week Number</label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="input-field col s12">
                                    {{ form.day }}
                                    <label for="dayNumber">Day Number</label>
                                </div>
                            </div>

                            <div class="row">
                                <div class="input-field col s12">
                                    {{ form.exercise }}
                                    <label for="exercise_name">Exercise Name</label>
                                </div>
                            </div>

                            <div class="row">
                                <div class="input-field col s12">
                                    {{ form.set_type }}
                                    <label for="set_type">Set Type</label>
                                </div>
                            </div>

                            <div class="row">
                                <div class="input-field col s12">
                                    {{ form.set }}
                                    <label for="set">Set Number</label>
                                </div>
                            </div>

                            <div class="row">
                                <div class="input-field col s12">
                                    {{ form.reps }}
                                    <label for="reps">Rep Number</label>
                                </div>
                            </div>


                            <div class="row">
                                <div class="input-field col s12">
                                    {{ form.routine }}
                                    <label for="routine">Routine</label>
                                </div>
                            </div>

                            <div class="row">
                                <div class="row">
                                    <div class="input-field col s12">
                                        <button class="btn waves-effect waves-light right trusique-red"
                                                type="submit" name="submit">Add Workout
                                            <i class="material-icons right">send</i>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

            <div class="col s12 m6 l8">
                <div class="card-panel">
                    <h4>Editing: {{ routine_name }}</h4>
                    <ul class="collapsible" data-collapsible="expandable">
                        {% for plan_week in plan_weeks %}

                            <li>
                                <div class="collapsible-header"><i
                                        class="material-icons">whatshot</i>{{ plan_week.week }}- Week
                                </div>
                                <div class="collapsible-body">
                                    <ul class="collapsible" data-collapsible="expandable">
                                        {% for plan_day in plan_days %}
                                            {% if plan_day.week == plan_week.week %}

                                                <li>
                                                    <div class="collapsible-header">{{ plan_day.day }}- day</div>
                                                    <!--collapsible workout name body -->
                                                    <div class="collapsible-body">
                                                        <ul class="collapsible" data-collapsible="expandable">
                                                            <!--begin workout name list-->

                                                            {% for plan_exercise in plan_exercise_name %}
                                                                {% for n in names %}
                                                                    {% for s in set_type %}
                                                                    {% if plan_day.day == plan_exercise.day and plan_week.week == plan_exercise.week and plan_exercise.exercise_id == n.id and plan_exercise.set_type_id == s.id%}
                                                                        <li>
                                                                            <div class="collapsible-header">{{ n.exercise_name }}-
                                                                                Excercise {{ s.type }}
                                                                            </div>
                                                                            <div class="collapsible-body">
                                                                                {% for setDetail in setDetails|dictsort:"set" %}
{#                                                                                    <p> setdetails exerceice {{ setDetail.exercise }}#}
{#                                                                                        plan excerice {{ plan_exercise.id }}</p>#}
                                                                                    {% if plan_day.day == setDetail.day and plan_week.week == setDetail.week and plan_exercise.exercise_id == setDetail.exercise and s.id == setDetail.set_type %}
                                                                                        <div class="row">
                                                                                            <div class="col s12 m4 l4">
                                                                                                Set {{ setDetail.set }}
                                                                                            </div>
                                                                                            <div class="col s12 m4 l4">
                                                                                                : {{ setDetail.reps }}Reps
                                                                                            </div>
                                                                                        </div>
                                                                                    {% endif %}
                                                                                {% endfor %}
                                                                            </div>
                                                                        </li>
                                                                    {% endif %}
                                                                    {% endfor %}
                                                                {% endfor %}
                                                            {% endfor %}
                                                        </ul>

                                                    </div>
                                                </li>
                                            {% endif %}
                                        {% endfor %}
                                    </ul>
                                </div>
                            </li>
                        {% endfor %}

                    </ul>
                </div>
            </div>

        </div>

        <!--end container-->
</section>
<!-- END CONTENT -->
{% endblock %}

0 个答案:

没有答案