好的,我一直在爬行谷歌和Django文檔超過2個小時(以及freenode上的IRC頻道),並沒有能夠找出這一個。如何使用ManyToMany字段填充默認表單數據?
基本上,我有一個名爲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)
我也得到了它具有以下的ModelForm代表我Room
模型forms.py:
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!
必須有一些其他的語法,我必須使用指定的「禁令」字段的默認值。我真的一直在拉我的頭髮,並肯定會感謝一些幫助。
謝謝!
UPDATE
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
個可選列表,與已經禁止來電者已經強調(選擇)
如果房間的編輯器只能編輯一個字段,只給他們在字段中編輯並將房間的其餘數據顯示爲顯示文本。這也將解決初始化M2M的缺省字段的問題 – 2010-06-06 09:22:32
你的意思是「它無法正常工作」?它產生了一個或是沒有任何東西? – 2010-06-06 15:23:11