I'm new to django and try to realize an project that allows the user to upload a file, parses it and enters the contained information into the same model:
class Track(models.Model):
Gpxfile = models.FileField("GPS XML", upload_to="tracks/gps/")
date=models.DateTimeField(blank=True)
waypoints = models.ForeignKey(Waypoint)
...
For the start I'm ok to work with the admin interface and to save work. So I hooked into the models save() method:
def save(self, *args, **kwargs):
"""we hook to analyse the XML files"""
super(Track, self).save(*args, **kwargs) #get the GPX file saved first
self.__parseGPSfile(self.Gpsxmlfile.path) #then analyse it
But here I run into problems due to the dependency:
- to get the filefield saved to a real file, I need to invoke the original save() first
- this breaks as some fields aren't populated yet as I didn't
- if I switch both lines, the file isn't saved yet and can't be parsed
Maybe I have just a lack of basic knowledge but even after reading a lot of SOs, blogs and googeling around, I don't have a clear idea how to solve it. I just found this ideas that don't seem to fit very well:
- create your own view and connect it to your handler (see here)
- bad as this won't work with admin interface, needs views, puts logic to views etc...
- use a validator for the filefield (see here)
- not sure if this is a good design, as it's about file processing in general and not realy validating (yet)
So what does the community suggest to realize a file postprocessing and data "import" in Django 1.4?