开发者

Question about ModelMultipleChoiceField

It so happened that I had to use arrays of PostgreSQL. In Django models do not have native support for arrays, so I used django_arrayfields. But for display in the admin should I use for the field hoprizontal_filter IntegerArrayField.

models.py

class Group(models.Model): 
    name = models.TextField()

class User(models.Model):
    name = models.TextField()
    groups = IntegerArrayField()

admin.py

class GroupAdminForm(forms.ModelForm):
    groups = forms.ModelMultipleChoiceField(
    queryset=Group.objects.all(), 
    label=('Select groups'),
    required=True,
    widget=FilteredSelectMultiple(
        ('groups'),
        False,
))

class UserAdmin(admin.ModelAdmin):
    fields = ('groups',)
    form = GroupAdminForm

As a result of this widget is displayed and works properly. But while maintaining writes can't ada开发者_开发问答pt type 'QuerySet'.

Please HELP!!!

UPD: Request information in the field POST variable groups has value which corresponds only to the last id of the selected group. Rather than an array as I expect.


Can't help without a proper error/traceback.

For your updated point, remember that you need to do request.POST.getlist(fieldname) if you're expecting multiple values.


so, first we need separate the thins:

admin.py

 from .forms import GroupAdminForm
class UserAdmin(admin.ModelAdmin):
    fields = ('groups',)
    form = GroupAdminForm

forms.py

class GroupAdminForm(forms.ModelForm):
    groups = forms.ModelMultipleChoiceField(
    queryset=Group.objects.all(), 
    label=('Select groups'),
    required=True,
    widget=FilteredSelectMultiple(
        ('groups'),
        False,
))

do this and post your trace error, it's hard to help without logs or error description.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜