Globally, we need call "add" form for model in admin site, but with options selected by user:
1. user checks items in "changelist" of objects RepoFile
2. select action "create job"
3. redirect user to standard "add" form for new object DeviceCronJob where field download_files already checked by selected files.
May be there is another way to do it. Variants that i tried is below.
I have add form for appropriate model. This model contains multichoice field:
models.py:class DeviceCronJob(models.Model):
...
download_files = models.TextField(max_length=250, blank=True, verbose_name=_("Files for downloading"))
forms.py:
class DeviceCronJobAddForm(forms.ModelForm):
download_files = forms.MultipleChoiceField(
widget=forms.CheckboxSelectMultiple(),
choices=[(file.hash, "%s [%s]" % (file.full_name,file.link)) for file in RepoFile.objects.filter()]
)
All work fine if i use standard admin "add".
I have other model RepoFile and customized Action: "Create download cronjob in template". On selecting files and pushing this action i need to pass selected files to DeviceCronJobAddForm.
i tried this:
admin.py
class DeviceCronJobAdmin(ModelAdmin):
model = DeviceCronJob
add_form = DeviceCronJobAddForm
list_display = ('cron_date', 'template', 'description', 'download_files', 'cron_user', 'cron_path', 'cron_mode', 'cdate')
...
def get_form(self, request, obj=None, **kwargs):
defaults = {}
setted = request.GET.get('setted','').split(",")
if obj is None: # if add new
if len(setted):
defaults.update({'form': self.add_form(setted=setted)})
else:
defaults.update({'form': self.add_form})
else: # if change view
defaults.update({
'form': self.add_form,
})
defaults.update(kwargs)
return super(DeviceCronJobAdmin, self).get_form(request, obj, **defaults)
models.py:
class DeviceCronJobAddForm(forms.ModelForm):
...
def __init__(self, *args, **kwargs):
files = kwargs.pop('setted','')
# actions with files
super(DeviceCronJobAddForm, self).__init__(*args, **kwargs)
but it raises error: unknown setted in super(DeviceCronJobAddForm, self).__init__(*args, **kwargs)
class DeviceCronJobAdmin(ModelAdmin):
model = DeviceCronJob
add_form = DeviceCronJobAddForm
list_display = ('cron_date', 'template', 'description', 'download_files', 'cron_user', 'cron_path', 'cron_mode', 'cdate')
...
def get_form(self, request, obj=None, **kwargs):
defaults = {}
setted = request.GET.get('setted','').split(",")
if obj is None: # if add new
if len(setted):
defaults.update({'form': self.add_form(setted=setted)})
else:
defaults.update({'form': self.add_form})
else: # if change view
defaults.update({
'form': self.add_form,
})
defaults.update(kwargs)
return super(DeviceCronJobAdmin, self).get_form(request, obj, **defaults)
models.py:
class DeviceCronJobAddForm(forms.ModelForm):
...
def __init__(self, *args, **kwargs):
files = kwargs.pop('setted','')
# actions with files
super(DeviceCronJobAddForm, self).__init__(*args, **kwargs)
but it raises error: unknown setted in super(DeviceCronJobAddForm, self).__init__(*args, **kwargs)
Also i tried pass selected files like GET arguments, but Form has not access to request object
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f0bafe2d-4ee7-4a65-a20d-553ffd8a63e9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment