Django内联表单集的初始数据

Sin*_*dex 8 python forms django inline-formset

我已经整理了一个表格以保存食谱.它使用表单和内联formset.我的用户使用包含食谱的文本文件,他们希望剪切和粘贴数据以使条目更容易.我已经研究了如何在处理原始文本输入后填充表单部分,但我无法弄清楚如何填充内联表单集.

似乎解决方案几乎在这里拼写出来:http: //code.djangoproject.com/ticket/12213但我不能把这些碎片放在一起.

我的模特:

#models.py

from django.db import models

class Ingredient(models.Model):
    title = models.CharField(max_length=100, unique=True)

    class Meta:
        ordering = ['title']

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return self.id

class Recipe(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    directions = models.TextField()

    class Meta:
        ordering = ['title']

    def __unicode__(self):
        return self.id

    def get_absolute_url(self):
        return "/recipes/%s/" % self.id

class UnitOfMeasure(models.Model):
    title = models.CharField(max_length=10, unique=True)

    class Meta:
        ordering = ['title']

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return self.id

class RecipeIngredient(models.Model):
    quantity = models.DecimalField(max_digits=5, decimal_places=3)
    unit_of_measure = models.ForeignKey(UnitOfMeasure)
    ingredient = models.ForeignKey(Ingredient)
    recipe = models.ForeignKey(Recipe)

    def __unicode__(self):
        return self.id
Run Code Online (Sandbox Code Playgroud)

配方表单使用ModelForm创建:

class AddRecipeForm(ModelForm):
    class Meta:
        model = Recipe
        extra = 0
Run Code Online (Sandbox Code Playgroud)

并且视图中的相关代码(调用解析表单输入被删除):

def raw_text(request):
    if request.method == 'POST':

    ...    

        form_data = {'title': title,
                    'description': description,
                    'directions': directions,
                    }

        form = AddRecipeForm(form_data)

        #the count variable represents the number of RecipeIngredients
        FormSet = inlineformset_factory(Recipe, RecipeIngredient, 
                         extra=count, can_delete=False)
        formset = FormSet()

        return render_to_response('recipes/form_recipe.html', {
                'form': form,
                'formset': formset,
                })

    else:
        pass

    return render_to_response('recipes/form_raw_text.html', {})
Run Code Online (Sandbox Code Playgroud)

如上所示,FormSet()为空,我可以成功启动页面.我已经尝试了几种方法来提供我确定的数量,unit_of_measure和成分,包括:

  • 设置初始数据但不适用于内联表单集
  • 传递字典,但会产生管理表单错误
  • init一起玩,但我有点超出我的深度

任何建议都非常感谢.

Ara*_*yan 20

我的第一个建议是采取简单的方法:保存RecipeRecipeIngredients,然后在生成Recipe时使用结果作为您的实例FormSet.您可能希望在配方中添加"已审核"布尔字段,以指示表单集是否随后被用户批准.

但是,如果您不想出于某种原因走这条路,您应该能够填充这样的formset:

我们假设你已经将文本数据解析为配方成分,并且有一个像这样的字典列表:

recipe_ingredients = [
    {
        'ingredient': 2,
        'quantity': 7,
        'unit': 1
    },
    {
        'ingredient': 3,
        'quantity': 5,
        'unit': 2
    },
]
Run Code Online (Sandbox Code Playgroud)

"成分"和"单位"字段中的数字是各个成分和度量对象单位的主要键值.我假设您已经制定了一些方法来将文本与数据库中的成分匹配,或者创建新的成分.

然后你可以这样做:

RecipeFormset = inlineformset_factory(
    Recipe,
    RecipeIngredient,
    extra=len(recipe_ingredients),
    can_delete=False)
formset = RecipeFormset()

for subform, data in zip(formset.forms, recipe_ingredients):
    subform.initial = data

return render_to_response('recipes/form_recipe.html', {
     'form': form,
     'formset': formset,
     })
Run Code Online (Sandbox Code Playgroud)

initial会将formset中每个表单的属性设置为recipe_ingredients列表中的字典.在显示formset方面似乎对我有用,但我还没有尝试过保存.