我有一个表单可以编辑我的模型实例。我想使用表单将所有隐藏的值传递给隐藏的username
的初始值默认为登录用户,以便它成为subscribe
表单。问题是正常initial={'field':value}
似乎不适用于许多领域。我该怎么办呢?
my views.py
@login_required
def event_view(request,eventID):
user = UserProfile.objects.get(pk=request.session['_auth_user_id'])
event = events.objects.get(eventID = eventID)
if request.method == 'POST':
form = eventsSusbcribeForm( request.POST,instance=event)
if form.is_valid():
form.save()
return HttpResponseRedirect('/events/')
else:
form = eventsSusbcribeForm(instance=event)
return render_to_response('event_view.html', {'user':user,'event':event, 'form':form},context_instance = RequestContext( request ))
my forms.py
class eventsSusbcribeForm(forms.ModelForm):
eventposter = forms.ModelChoiceField(queryset=UserProfile.objects.all(), widget=forms.HiddenInput())
details = forms.CharField(widget=forms.Textarea(attrs={'cols':'50', 'rows':'5'}),label='Enter Event Description here')
date = forms.DateField(widget=SelectDateWidget())
class Meta:
model = events
exclude = ('deleted')
def __init__(self, *args, **kwargs):
super(eventsSusbcribeForm, self).__init__(*args, **kwargs)
self.fields['username'].initial = (user.id for user in UserProfile.objects.filter())
my models.py
class events(models.Model):
eventName = models.CharField(max_length=100)
eventID = models.AutoField(primary_key=True)
details = models.TextField()
attendanceFee = models.FloatField(max_length=99)
date = models.DateField()
username = models.ManyToManyField(UserProfile, related_name='user', blank=True)
eventposter = models.ForeignKey(UserProfile, related_name='event_poster')
deleted = models.BooleanField()
def __unicode__(self):
return u'%s' % (self.eventName)
答案 0 :(得分:8)
您可以发布您的活动模型吗?如果没有它,你很难猜到你想要做什么。没有它,我必须假设一些事情,所以如果我错了,我很抱歉。
首先,我猜你不应该为EventSubscriptionForm使用Event ModelForm。这没有多大意义。希望您为Event和User创建了一个直通类,因此在您的Event模型中,您有类似
的内容subscriber_users = models.ManyToManyField(User, through="Subscription")
和
class Subscription(models.Model):
user = models.ForeignKey(User, related_name="events",)
event = models.ForeignKey(Event, related_name="subscribers")
然后您可以使用Subscription ModelForm。
你有没有理由使用eventID而不是django idiom,event_id?您还应该使用Pythonic外壳导入Event和EventSubcribeForm类。一个非常重要的是你应该将所有内容链接到User而不是UserProfile。
从技术上讲,在视图中设置initial而不是 init 形式更有意义,因为无论如何你必须将request.user传递给 init 。
我认为你应该试试这个......
@login_required
def event_view(request, event_id=None):
user = request.user.get_profile()
event = Event.objects.get(id=event_id)
initial = {'user': request.user}
form = EventSubcriptionForm(request.POST or None, instance=event, initial=initial)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('event_list'))
return render_to_response('event_view.html', {
'event': event,
'form': form
}, context_instance = RequestContext(request))
一些注意事项
request.POST or None
来避免request.method个案user
,只需设置上下文处理器(请参阅pinax,例如有关如何执行此操作),而不是在每个视图中传递它。您也可以随时使用request.user。请记住,此代码只有在您按照我所说的那样通过类设置并使用类似
的表单时才有效class EventSubcriptionForm(forms.ModelForm):
class Meta:
model = Subscription
exclude = ('event')
修改强> 非常感谢一群人。我不是django的新手,但不知何故对SO来说是新手。
好的,你应该真正阅读一些关于Python约定http://www.python.org/dev/peps/pep-0008/的PEP或关于它的一些SO帖子What is the naming convention in Python for variable and function names?。
以下是我推荐给您的活动应用程序models.py:
class Event(models.Model):
name = models.CharField(max_length=100)
details = models.TextField()
attendance_fee = models.FloatField(max_length=99)
date = models.DateField()
poster = models.ForeignKey(User, related_name='events_posted')
deleted = models.BooleanField()
attendee_users = models.ManyToManyField(User, through="Attendance")
def __unicode__(self):
return self.name
class Attendance(models.Model):
user = models.ForeignKey(User, related_name="events",)
event = models.ForeignKey(Event, related_name="attendees")
注释
现在,您可以使用event.attendees访问参加活动的用户。
答案 1 :(得分:1)
我在尝试为manytomany设置默认值时发现了这一点。我不想添加直通表。
基于Casey发布的视图,但是在多种关系中添加了用户。
初始帖子:
@login_required
def event_view(request, event_id=None):
user = request.user.get_profile()
event = Event.objects.get(id=event_id)
initial = {'user': request.user, 'username': [ request.user.id, ] } # makes the poster also an attendee
form = EventSubcriptionForm(request.POST or None, instance=event, initial=initial)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('event_list'))
return render_to_response('event_view.html', {
'event': event,
'form': form
}, context_instance = RequestContext(request))
更新版本:
@login_required
def event_view(request, event_id=None):
user = request.user.get_profile()
event = Event.objects.get(id=event_id)
initial = {'user': request.user, 'subscriber_users': [ request.user.id, ] } # makes the poster also an subscriber
form = EventSubcriptionForm(request.POST or None, instance=event, initial=initial)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('event_list'))
return render_to_response('event_view.html', {
'event': event,
'form': form
}, context_instance = RequestContext(request))