Django: Initializing a FormSet of custom forms with instances


Question


Given the following models:

class Graph(models.Model):
    owner = models.ForeignKey(User)

    def __unicode__(self):
        return u'%d' % self.id

class Point(models.Model):
    graph = models.ForeignKey(Graph) 
    date  = models.DateField(primary_key = True)
    abs   = models.FloatField(null = True)
    avg   = models.FloatField(null = True)

    def __unicode__(self):
        return u'%s' % self.date

I am trying to create a form for editing lists of Points. The HTML input tags require additional attributes to be set, so I am using the following custom form:

class PointForm(forms.ModelForm):
    graph = forms.ModelChoiceField(queryset = Graph.objects.all(),
                                   widget   = forms.HiddenInput())
    date  = forms.DateField(widget = forms.HiddenInput(), label = 'date')
    abs   = forms.FloatField(widget = forms.TextInput(
                                      attrs = {'class': 'abs-field'}),
                            required = False)

    class Meta:
        model  = Point
        fields = ('graph', 'date', 'abs')  # Other fields are not edited.

    def pretty_date(self):
        return self.data.strftime('%B')

At this point I do not know how to pass instances of the Point class to a FormSet:

def edit(request):
    PointFormSet = forms.formsets.formset_factory(PointForm, extra = 0)
    if request.method == 'POST':
        return

    # Receive 3 points to edit from the database.
    graph, res = Graph.objects.get_or_create(id = 1)
    one_day    = datetime.timedelta(days = 1)
    today      = datetime.date.today()
    do_edit    = []
    for date in [today - (x * one_day) for x in range(3)]:
        point, res = Point.objects.get_or_create(graph = graph, date = date)
        do_edit.append(point)

    formset = PointFormSet(????) # How is this initialized with the points?

I found a hack that somewhat works, but it leads to errors later on when trying to process the resulting POST data:

do_edit = []
for date in [today - (x * one_day) for x in range(3)]:
    point, res    = Point.objects.get_or_create(graph = graph, date = date)
    data          = point.__dict__.copy()
    data['graph'] = graph
    do_edit.append(data)

formset = PointFormSet(initial = do_edit)

How is this done correctly?

For the reference, my template looks like this:

<form action="" method="post">
{{ formset.management_form }}
<table>
    <tbody>
    {% for form in formset.forms %}
        <tr>
            <td>{{ form.graph }} {{ form.date }} {{ form.pretty_date }}:</td>
            <td width="100%">{{ form.abs }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>
</form>

Answer 1:


The trick is to use a "ModelFormset" instead of just a formset since they allow initialization with a queryset. The docs are here, what you do is provide a form=* when creating the model formset and queryset=* when your instantiating the formset. The form=* arguement is not well documented (had to dig around in the code a little to make sure it is actually there).

def edit(request):
    PointFormSet = modelformset_factory(Point, form = PointForm)
    qset = Point.objects.all() #or however your getting your Points to modify
    formset = PointFormset(queryset = qset)
    if request.method == 'POST':
        #deal with posting the data
        formset = PointFormset(request.POST)
        if formset.is_valid():
            #if it is not valid then the "errors" will fall through and be returned
            formset.save()
        return #to your redirect

    context_dict = {'formset':formset,
                    #other context info
                    }

    return render_to_response('your_template.html', context_dict)

So the code walks through easily. If the request is a GET then the instantiated form is returned to the user. If the request is a POST and the form is not .is_valid() then the errors "fall through" and are returned in the same template. If the request is a POST and the data is valid then the formset is saved.

Hope that helps.

-Will




Answer 2:


If you only have one possible value which you want to set, or perhaps a closed of values, it is possible to set them after the user POSTS the data to your server using commit=False

Please consider the following code:

class UserReferralView(View):
    ReferralFormSet = modelformset_factory(ReferralCode,
                                           form=ReferralTokenForm, extra=1)

    def get(self, request):
        pass

    def post(self, request):
        referral_formset = UserUpdateView.ReferralFormSet(request.POST)

        if referral_formset.is_valid():
            instances = referral_formset.save(commit=False)
            for instance in instances:
                instance.user = request.user
                instance.save()
            return redirect(reverse('referrals.success_view'))
         else:
            return redirect(reverse('referrals.failure_view'))


来源:https://stackoverflow.com/questions/1992152/django-initializing-a-formset-of-custom-forms-with-instances


码神部落- 版权声明 1、本主题所有言论和图片纯属会员个人意见,与码神部落立场无关。
2、本站所有主题由该帖子作者发表,该帖子作者这爷绝蝂码神部落享有帖子相关版权。
3、码神部落管理员和版主有权不事先通知发贴者而删除本文。
4、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者这爷绝蝂码神部落的同意。
5、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任。
6、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责。
7、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意。

最新回复 (0)
    • 码神部落
      2
        立即登录 立即注册 GitHub登录
返回
发新帖
作者最近主题: