Felix
I try to follow the ideas, but i feel like taking the dirty way.
Here's my work:
Because i can't replace the modelform created by the admin for the
"Data" model with a standard form as the Django docs says, i created a
view to replace the "add" view generated by the admin and put a
standard form:
class DataAdmin(admin.ModelAdmin):
change_form_template = 'admin/data/data/change_form.html'
def get_urls(self):
urls = super(DataAdmin, self).get_urls()
my_urls = patterns('',
('^add/$',
self.admin_site.admin_view(self.add_view)),
)
return my_urls + urls
def add_view(self, request, extra_content=None):
FormInput = DataInput()
context = {'form': FormInput}
return super(DataAdmin, self).add_view(request,
extra_context=context)
And the form is:
import csv
from datetime import datetime
class DataInput(forms.Form):
file = forms.FileField()
place = forms.ModelChoiceField(queryset=Places.objects.all())
def save(self):
records = csv.reader(self.cleaned_data["file"])
for line in records:
input_data = Data()
input_data.place = self.cleaned_data["place"]
input_data.time = datetime.strptime(line[1], "%m/%d/%y %H:
%M:%S")
input_data.data_1 = line[2]
input_data.data_2 = line[3]
input_data.data_3 = line[4]
input_data.save()
In the template are the "multipart/data" and "csrftoken" orders.
But again... this is a karma.... the IntegrityError: datas_data.time
may not be NULL
As i say at the beginning, i feel like taking the dirty way, and
obviously not seeing something.
Regards!
On Oct 30, 5:55 pm, Felix Dreissig <f...@f30.me> wrote:
> The ordinary user won't have to deal with the command line, you just
> need to get the CSV file. A ModelForm to which an FileField is added
> doesn't really have anything to do with that.
>
> Instead, you should create a standard form with a single FileField to
> upload the CSV file, save it temporarily or just keep it as a file
> object and then process it as Jirka pointed out.
>
> Regards,
> Felix
>
> On 29.10.2010 18:31, Jorge wrote:
>
> > Jirka
>
> > I need an easy method inside the admin to upload the csv file with an
> > web interface. I guess with your method the admin will need access to
> > the server and the command line, and something like this is not what i
> > try to do, because the admin (not me) is not a django developer, not
> > even a user get used to command lines. But if my guessing is wrong how
> > can i do a web interface for your method?
>
> > Regards!
>
> > On Oct 29, 5:44 am, Jirka Vejrazka <jirka.vejra...@gmail.com> wrote:
> >> I must still be missing something here. If all you want to do is to
> >> read CSV file and save the data in the database, I still don't
> >> understand why you use the forms machinery.
>
> >> You only *need* to use the data model, e.g.
>
> >> from myapp.models import Data
>
> >> csvfile = csv.reader('something.csv')
>
> >> for line in csvfile:
> >> do_necessary_data_conversions_or_checks(line)
> >> try:
> >> Data.objects.create(field1=line[0], field2=line[1], ...)
> >> except (IndexError, IntegrityError):
> >> print 'Invalid line encountered:\n%s' % line
>
> >> What am I missing?
>
> >> Jirka
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.