In my Django model, I have two fields for latitude and longitude which are both declared as CharField. The model needs to accept multiple coordinates, so in the UI form, I enter these coordinates separated by commas. I then split this char input in a function to perform computations.
This is what my models.py looks like:
class Location(models.Model):
country = models.ForeignKey(Countries, on_delete=models.CASCADE)
latitude = models.CharField(max_length=1000, help_text="Add the latitudes followed by comma")
longitude = models.CharField(max_length=1000, help_text="Add the longitudes followed by comma")
This is the function in my view.py:
def dashboard(request):
form = LocationForm
if request.method == 'POST':
form = LocationForm(request.POST)
if form.is_valid():
form.save()
latitude = list(map(float, form.cleaned_data.get('latitude').split(',')))
longitude = list(map(float, form.cleaned_data.get('longitude').split(',')))
.........
.........
return render(request, dashboard/dashboard.html, { 'form':form })
Now, I want to be able to add coordinates using a CSV file in addition to manually entering them. I'd like an option in the UI to upload a CSV file containing multiple latitudes and longitudes which will then populate the respective char fields with comma-separated values.
It's important to note that the CSV file won't include the country name, which will still need to be entered through the UI form.
To clarify, I only want some of the form fields to be populated by the CSV file, not all of them. Existing solutions I've found populate all model fields including those not present in the CSV file, creating a dependency issue.
What can I do in this case? Maybe introduce a FileField() in the model or utilize JavaScript to handle coordinate population in the UI... or explore other options?
If you're unsure about the best approach, I'm open to suggestions!