如何使用ManyToMany字段填充默认表单数据?

时间:2010-06-06 05:10:30

标签: python django django-forms

好的,我现在已经抓取了谷歌和Django文档超过2个小时(以及freenode上的IRC频道),并且无法解决这个问题。

基本上,我有一个名为Room的模型,显示如下:

class Room(models.Model):
    """
    A `Partyline` room. Rooms on the `Partyline`s are like mini-chatrooms. Each
    room has a variable amount of `Caller`s, and usually a moderator of some
    sort. Each `Partyline` has many rooms, and it is common for `Caller`s to
    join multiple rooms over the duration of their call.
    """
    LIVE = 0
    PRIVATE = 1
    ONE_ON_ONE = 2
    UNCENSORED = 3
    BULLETIN_BOARD = 4
    CHILL = 5
    PHONE_BOOTH = 6
    TYPE_CHOICES = (
        ('LR', 'Live Room'),
        ('PR', 'Private Room'),
        ('UR', 'Uncensored Room'),
    )

    type = models.CharField('Room Type', max_length=2, choices=TYPE_CHOICES)
    number = models.IntegerField('Room Number')
    partyline = models.ForeignKey(Partyline)
    owner = models.ForeignKey(User, blank=True, null=True)
    bans = models.ManyToManyField(Caller, blank=True, null=True)

    def __unicode__(self):
        return "%s - %s %d" % (self.partyline.name, self.type, self.number)

我还有一个forms.py,它有以下ModelForm来表示我的Room模型:

from django.forms import ModelForm

from partyline_portal.rooms.models import Room


class RoomForm(ModelForm):
    class Meta:
        model = Room

我正在创建一个允许管理员编辑给定Room对象的视图。这是我的观点(到目前为止):

def edit_room(request, id=None):
    """
    Edit various attributes of a specific `Room`. Room owners do not have
    access to this page. They cannot edit the attributes of the `Room`(s) that
    they control.
    """
    room = get_object_or_404(Room, id=id)
    if not room.is_owner(request.user):
        return HttpResponseForbidden('Forbidden.')

    if is_user_type(request.user, ['admin']):
        form_type = RoomForm
    elif is_user_type(request.user, ['lm']):
        form_type = LineManagerEditRoomForm
    elif is_user_type(request.user, ['lo']):
        form_type = LineOwnerEditRoomForm

    if request.method == 'POST':
        form = form_type(request.POST, instance=room)
        if form.is_valid():
            if 'owner' in form.cleaned_data:
                room.owner = form.cleaned_data['owner']

        room.save()
    else:
        defaults = {'type': room.type, 'number': room.number, 'partyline': room.partyline.id}
        if room.owner:
            defaults['owner'] = room.owner.id
        if room.bans:
            defaults['bans'] = room.bans.all() ### this does not work properly!

        form = form_type(defaults, instance=room)

    variables = RequestContext(request, {'form': form, 'room': room})
    return render_to_response('portal/rooms/edit.html', variables)

现在,当我查看页面时,此视图正常工作。它显示了所有表单属性,并填写了所有默认值(当用户执行GET时)...除了ManyToMany字段'禁止'的默认值外。

基本上,如果管理员点击要编辑的Room对象,他们转到的页面将显示除“禁令”之外的所有Room默认值。无论我做什么,我都找不到让Django显示Room对象的当前“禁用用户”的方法。以下是需要更改的代码行(从视图中):

defaults = {'type': room.type, 'number': room.number, 'partyline': room.partyline.id}
if room.owner:
        defaults['owner'] = room.owner.id
if room.bans:
        defaults['bans'] = room.bans.all() ### this does not work properly!

我必须使用其他一些语法来指定'禁令'字段的默认值。我真的一直把头发拉出来,肯定会感激一些帮助。

谢谢!

更新

lazerscience实际上帮助我在他的一条评论中找到了解决方案。基本上,它的工作方式是传递主键列表。为了使它工作,我不得不改变:

if room.bans:
        defaults['bans'] = room.bans.all() ### this does not work properly!

if room.bans:
        defaults['bans'] = [b.pk for b in room.bans.all()]

然后bam,它立即开始工作(当我查看页面时,它将显示Caller的可选列表,已经被禁止的呼叫者已经突出显示(选中)。

1 个答案:

答案 0 :(得分:0)

您可能需要使用“initial”:Django set default form values