modelformset 字段中的限制值 [英] Limit values in the modelformset field

查看:21
本文介绍了modelformset 字段中的限制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试解决这个问题好几天了,变得非常绝望.有关我尝试过但没有奏效的一些内容,请参阅注释掉的代码片段.

I've been trying to solve this problem for a couple of days now, getting quite desperate. See the commented out code snippets for some of the things I've tried but didn't work.

问题: 如何将 IngredientFormcategory 字段中的值限制为仅属于当前登录用户的值?

Problem: How can I limit the values in the category field of the IngredientForm to only those belonging to the currently logged in user?

views.py

@login_required
def apphome(request):
    IngrFormSet = modelformset_factory(Ingredient, extra=1, fields=('name', 'category'))

    # Attempt #1 (not working; error: 'IngredientFormFormSet' object has no attribute 'fields')
    # ingrformset = IngrFormSet(prefix='ingr', queryset=Ingredient.objects.none())
    # ingrformset.fields['category'].queryset = Category.objects.filter(user=request.user)

    # Attempt #2 (doesn't work)
    # ingrformset = IngrFormSet(prefix='ingr', queryset=Ingredient.objects.filter(category__user_id = request.user.id))

models.py:

class Category(models.Model):
    name = models.CharField(max_length=30, unique=True)
    user = models.ForeignKey(User, null=True, blank=True)       

class Ingredient(models.Model):
    name = models.CharField(max_length=30, unique=True)
    user = models.ForeignKey(User, null=True, blank=True)
    category = models.ForeignKey(Category, null=True, blank=True)
    counter = models.IntegerField(default=0)

forms.py:

class IngredientForm(ModelForm):
    class Meta:
        model = Ingredient 
        fields = ('name', 'category')

更新:我已经取得了一些进展,但该解决方案目前是硬编码的,并不真正可用:

UPDATE: I've made some progress but the solution is currently hard-coded and not really usable:

我发现我可以通过表单类控制category表单字段,然后像这样在视图中传递表单:

I found out I can control the categoryform field via form class and then pass the form in the view like this:

#forms.py
class IngredientForm(ModelForm):
    category = forms.ModelChoiceField(queryset = Category.objects.filter(user_id = 1))

    class Meta:
        model = Ingredient 
        fields = ('name', 'category')

#views.py
IngrFormSet = modelformset_factory(Ingredient, form = IngredientForm, extra=1, fields=('name', 'category'))

上面产生了我需要的结果,但显然 user 是硬编码的.我需要它是动态的(即当前用户).我尝试了一些访问 forms.py 中的 request.user 的解决方案,但没有奏效.

The above produces the result I need but obviously the user is hardcoded. I need it to be dynamic (i.e. current user). I tried some solutions for accessing the request.user in forms.py but those didn't work.

任何想法如何前进?

推荐答案

您不需要任何类型的自定义表单.您可以将 category 字段的查询集更改为:

You don't need any kind of custom forms. You can change the queryset of category field as:

IngrFormSet = modelformset_factory(Ingredient, extra=1, fields=('name', 'category'))
IngrFormSet.form.base_fields['category'].queryset = Category.objects.filter(user__id=request.user.id)

这篇关于modelformset 字段中的限制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆