parsing data from fileupload
How do I add data from a fileupload to a database? For example, if I have a Book model that is for names of books and a FileNames model that is used on a form, how do I populate Book.title. class FileNames(models.Model): title = models.FileField(upload_to='tmp', blank=False) class Book(models.Model): title = models.CharField(max_length=30) I'm able to upload and parse the file. I'm just not sure how to add the parsed data to Book. Thanks --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: parsing data from fileupload
2009/5/17 Patrick : > > How do I add data from a fileupload to a database? > For example, if I have a Book model that is for names of books and a > FileNames model that is used on a form, how do I populate Book.title. > > class FileNames(models.Model): > title = models.FileField(upload_to='tmp', blank=False) > > > class Book(models.Model): > title = models.CharField(max_length=30) > > I'm able to upload and parse the file. I'm just not sure how to add > the parsed data to Book. > So FileNames is not a real table? I'm confused ... Please check http://docs.djangoproject.com/en/dev/topics/http/file-uploads/ it would help you a lot to deal with file uploads. -- Antoni Aloy López Blog: http://trespams.com Site: http://apsl.net --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: parsing data from fileupload
Filenames is also a table, but I'm interested in the data withing the file. On May 17, 2:38 pm, Antoni Aloy wrote: > 2009/5/17 Patrick : > > > How do I add data from a fileupload to a database? > > For example, if I have a Book model that is for names of books and a > > FileNames model that is used on a form, how do I populate Book.title. > > > class FileNames(models.Model): > > title = models.FileField(upload_to='tmp', blank=False) > > > class Book(models.Model): > > title = models.CharField(max_length=30) > > > I'm able to upload and parse the file. I'm just not sure how to add > > the parsed data to Book. > > So FileNames is not a real table? I'm confused ... > > Please checkhttp://docs.djangoproject.com/en/dev/topics/http/file-uploads/ > it would help you a lot to deal with file uploads. > > -- > Antoni Aloy López > Blog:http://trespams.com > Site:http://apsl.net --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: parsing data from fileupload
On Sun, May 17, 2009 at 5:39 PM, Patrick wrote: > > Filenames is also a table, but I'm interested in the data withing the > file. > > On May 17, 2:38 pm, Antoni Aloy wrote: > > 2009/5/17 Patrick : > > > > > How do I add data from a fileupload to a database? > > > For example, if I have a Book model that is for names of books and a > > > FileNames model that is used on a form, how do I populate Book.title. > > > > > class FileNames(models.Model): > > >title = models.FileField(upload_to='tmp', blank=False) > > > > > class Book(models.Model): > > >title = models.CharField(max_length=30) > > > > > I'm able to upload and parse the file. I'm just not sure how to add > > > the parsed data to Book. > > > > So FileNames is not a real table? I'm confused ... > > > > Please checkhttp:// > docs.djangoproject.com/en/dev/topics/http/file-uploads/ > > it would help you a lot to deal with file uploads. > > > > -- > > Antoni Aloy López > > Blog:http://trespams.com > > Site:http://apsl.net > > > Getting an instance of a FileName and accessing .title on it will give you a file like object you can work with normally and extract whatever information you want from it. I'm not 100% sure I understand your question. Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: parsing data from fileupload
Well, I guess what I'm confused about is adding data that doesn't come directly from forms. I ended up doing this and it seemed to work. for line in file: b=Book() b.title=line b.save() Thanks On May 17, 3:41 pm, Alex Gaynor wrote: > On Sun, May 17, 2009 at 5:39 PM, Patrick wrote: > > > Filenames is also a table, but I'm interested in the data withing the > > file. > > > On May 17, 2:38 pm, Antoni Aloy wrote: > > > 2009/5/17 Patrick : > > > > > How do I add data from a fileupload to a database? > > > > For example, if I have a Book model that is for names of books and a > > > > FileNames model that is used on a form, how do I populate Book.title. > > > > > class FileNames(models.Model): > > > > title = models.FileField(upload_to='tmp', blank=False) > > > > > class Book(models.Model): > > > > title = models.CharField(max_length=30) > > > > > I'm able to upload and parse the file. I'm just not sure how to add > > > > the parsed data to Book. > > > > So FileNames is not a real table? I'm confused ... > > > > Please checkhttp:// > > docs.djangoproject.com/en/dev/topics/http/file-uploads/ > > > it would help you a lot to deal with file uploads. > > > > -- > > > Antoni Aloy López > > > Blog:http://trespams.com > > > Site:http://apsl.net > > Getting an instance of a FileName and accessing .title on it will give you a > file like object you can work with normally and extract whatever information > you want from it. I'm not 100% sure I understand your question. > > Alex > > -- > "I disapprove of what you say, but I will defend to the death your right to > say it." --Voltaire > "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: parsing data from fileupload
On May 18, 2009, at 12:12 PM, Patrick wrote: > > Well, I guess what I'm confused about is adding data that doesn't come > directly from forms. > I ended up doing this and it seemed to work. > > for line in file: >b=Book() >b.title=line >b.save() Here is how I do it, Model, Form, View. The Django file form can process a bunch of this but I am capturing some metadata that is NOT on the forms so have to do that after form processing then do a separate save(). Note that my models are inheriting from some abstract base classes which provide tracking and hierarchy relationships, but you can see what the upload part is doing fairly clearly. The model: class File(FileDropContent): file= models.FileField(upload_to=FILEDROP_ROOT) mime_type = models.CharField(_('mime_type'), max_length=127) downloaded = models.DateTimeField(_('last download date'), null=True) expiration = models.DateTimeField(_('expiration date'),) parent = models.ForeignKey(Container, related_name='files', null=True) def is_expired(self): return self.expiration < datetime.datetime.now() The form: class FileForm(forms.ModelForm): """ Upload File, select file from filesystem browser, get expriation description from user. """ def __init__(self, *args, **kwargs): """ Fix field order here too. """ super(FileForm, self).__init__(*args, **kwargs) description = self.fields.pop('description') new_pos = len(self.fields) self.fields.insert(new_pos, 'description', description) class Meta: model = File fields = ['file', 'expiration', 'description'] And the view: def file_create(request, parent_id): """ Get upload file and metadata (e.g. true name), save file to disk. """ parent = Container.objects.get(pk=parent_id) if not _is_writable(request.user, parent): return HttpResponse("You don't have permission to upload a file into parent '%s'" % parent) if request.method == "POST": form = FileForm(request.POST, request.FILES) if form.is_valid(): uploadobj = form.save(commit=False) uploadobj.parent= parent uploadobj.title = request.FILES['file'].name uploadobj.mime_type = request.FILES['file'].content_type uploadobj.creator = request.user uploadobj.ip= request.META.get('REMOTE_ADDR', "0.0.0.0") uploadobj.save() return HttpResponseRedirect(reverse("file_detail", args=[uploadobj.pk])) --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: parsing data from fileupload
Oh, make sure your upload form specifies the 'enctype' attribute so it will actually send a file object, like: {% block content %} Upload a file... {{ form.as_p }} {% endblock %} --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---