我在class ActivityGroupInline(InlineFormSet):
model = models.ActivityGroup
fields = ['events',]
can_delete = False
extra = 0
@property
def widgets(self):
return {
'events': widgets.CheckboxSelectMultiple(),
}
def get_factory_kwargs(self):
kwargs = super().get_factory_kwargs()
kwargs.update({
'widgets': self.widgets,
})
return kwargs
class ActivityGroupEventLink(NamedFormsetsMixin, UpdateWithInlinesView):
model = models.Container
fields = []
template_name = 'main/link_activitygroupsevents.html'
success_url = reverse_lazy('dashboard')
inlines = [ActivityGroupInline,]
inlines_names = ['activitygroup_inline',]
def get_object(self, queryset=None):
return models.Container.objects.first()
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['breadcrumbs'] = [
{'name': 'Home', 'url': reverse_lazy('dashboard')},
{'name': 'Link Activities to Events', 'is_active': True}
]
all_events = models.Event.objects.filter(
container=context['object'],
).all()
context['events'] = { event.id: event for event in all_events }
for form in context['activitygroup_inline'].forms:
activitygroup_kcs = set(form.instance.key_characteristics.all())
potential_event_ids = set()
for event in all_events:
event_kcs = set(event.key_characteristics.all())
if event_kcs & activitygroup_kcs:
potential_event_ids.add(event.id)
form.fields['events'].choices = models.Event.objects.filter(
id__in=list(potential_event_ids)
).order_by('id').values_list('id', 'name')
return context
def forms_valid(self, form, inlines):
self.object = form.save()
for formset in inlines:
instances = formset.save(commit=False)
for inst in instances:
initial_events = [] # This is code I added to work with the solution that I am now trying
new_events = []
for f in formset:
initial_events.append(f.initial['events']) # This is code I added to work with the solution that I am now trying
new_events.append(f.cleaned_data['events'].first())
flat_initial = [item for sublist in initial_events for item in sublist] # This is code I added to work with the solution that I am now trying
try:
event = next(event for event in new_events if event is not None) # This is what I was previously doing to find the relevant event but it only works if there is one column in the table.
except StopIteration:
raise ValidationError('There must be at least one Activity Group linked to an event.')
activityevent = inst.activityevent_set.filter(activity_group_id=inst.id, event_id=event.id)
if activityevent.exists():
activityevent.delete()
else:
new_activityevent = models.ActivityEvent.objects.create(
activity_group=inst,
event=event,
)
new_activityevent.save()
inst.save()
return HttpResponseRedirect(self.get_success_url())
的{{1}}中进行了一项活动,如下图所示:
此外,用户可以通过单击添加其他按钮添加任意数量的项目。如果用户转到另一活动,然后再次转到该活动,则listview
必须以用户保存它的方式出现。我使用共享首选项来保存已选中和未选中的首选项。但是该列表可能会变得很长,因此使用其他任何方法会更好吗?
答案 0 :(得分:0)
您正在使用ListView
。
这意味着您已经有了某种ArrayList
来存储标签:如我所见,它是FirstItem,SecondItem,...和Description。
在此ListView的适配器中,创建一个名为isChecked的布尔字段,并在其中存储每个复选框的状态。
因此适配器将保留3个字段,而不是2个字段。
答案 1 :(得分:0)
如果列表可能很长,并且每次打开时都需要重新创建它,则最好将此数据存储在某些本地数据库中,例如room persistence library。
SharedPreferences
设计用于存储少量数据,例如您的案例中有关某一特定项目的信息。