我有以下模型:主题,UserProfile,UserSubscribedToTopic
最后一个看起来像这样:
class UserSubscribedToTopic(models.Model):
topic = models.ForeignKey(Topic)
user_profile = models.ForeignKey(UserProfile)
start_date = models.DateField(null=True, blank=True)
我想向用户显示一个主题列表,每个主题都有一个复选框。如果用户选中了复选框,那么我将使用JavaScript来显示“开始日期”文本字段(因此,出于此问题的目的,我只需要在复选框旁边显示一个文本字段)。如果用户已经保存了他们的选择并且正在重新访问该页面,我想在首次呈现时相应地填充该表单。
我尝试使用formsets:
class SubscribeToTopicForm(ModelForm):
class Meta:
model = UserSubscribedToTopic
fields = ('topic','start_date')
widgets = {'topic': CheckboxInput(attrs={'class': 'topic-checkbox'}),
'start_date': TextInput(attrs={'class': 'date','placeholder': 'Start date'})}
SubscribeToTopicFormSetBase = modelformset_factory(
UserSubscribedToTopic,
form=SubscribeToTopicForm,
extra = 0)
class SubscribeToTopicFormSet(SubscribeToTopicFormSetBase):
def add_fields(self, form, index):
super(SubscribeToTopicFormSet, self).add_fields(form, index)
如果我将以下内容添加到我的视图中,我几乎得到了我想要的内容:
topics_formset = SubscribeToTopicFormSet(queryset=UserSubscribedToTopic.objects.filter(user_profile=user.get_profile()))
但是,显然这只会显示用户已订阅的主题。要显示我真正需要做的所有主题是Topic表上的LEFT JOIN。我不知道如何在不使用raw的情况下在Django中执行此操作。
我的问题:
答案 0 :(得分:0)
您应该在Topic
模型上创建表单,然后使用user_set
管理器查看当前用户是否订阅了该主题。
提交表单后,如果选中任何字段,则可以在视图中创建单独的UserSubscribedToTopic
个对象。
答案 1 :(得分:0)
我最终将日期字段中的复选框分开,因此我可以使用Form中的forms.ModelMultipleChoiceField和手动创建的formset来处理日期字段。
以下是代码:
形式:
class SubscribedToTopicForm(ModelForm):
subscribed_to_topic = forms.ModelMultipleChoiceField(required=False,queryset=Topic.available_topics, widget=forms.CheckboxSelectMultiple(attrs={'class': 'topic-checkbox'}))
class Meta:
model = UserProfile
fields = ("subscribed_to_topic",)
def get_checked_topics(self):
return self.cleaned_data['subscribed_to_topic']
class StartDateForm(forms.Form):
topic_id = forms.CharField(widget=forms.HiddenInput,required=False)
start_date = forms.DateField(required=False,label='')
StartDateFormSetBase = formset_factory(form=StartDateForm,extra = 0)
class StartDateFormSet(StartDateFormSetBase):
def get_start_date(self, topic_id):
for i in range(0, self.total_form_count()):
form = self.forms[i]
form_topic_id=long(form.cleaned_data['topic_id'])
if topic_id == form_topic_id:
return form.cleaned_data['start_date']
return ''
查看:
GET:
topics_form = SubscribedToTopicForm()
subscribed_to_topic=None
if request.user.is_authenticated():
subscribed_to_topics = SubscribedToTopic.objects.filter(user_profile=request.user.get_profile())
initial_data = []
for topic in Topic.available_topics.all():
start_date=''
if subscribed_to_topics:
for subscribed_to_topic in subscribed_to_topics:
if subscribed_to_topic.topic.id==topic.id:
start_date=subscribed_to_topic.start_date
initial_data.append({'topic_id':topic.id, 'start_date': start_date})
start_date_formset = StartDateFormSet(initial=initial_data)
POST:
start_date_formset = StartDateFormSet(request.POST)
topics_form = SubscribedToTopicForm(request.POST)
start_date_formset.is_valid()
topics_form.is_valid()
for topic in topics_form.get_checked_topics():
start_date = start_date_formset.get_start_date(topic.id)
subscribed_to_topic = SubscribedToTopic()
subscribed_to_topic.topic=topic
subscribed_to_topic.start_date=start_date
subscribed_to_topic.save()