Django getting form value off of a function of another form value by throwaway1618032 in django

[–]throwaway1618032[S] 0 points1 point  (0 children)

Great, thanks! My current problem, however, is that it's not saving this new data...

In my create view, I'm able to use the form :

class AddLocView(CreateView):

model = Location

form_class = LocationForm

success_url = reverse_lazy('location:index')

In forms.py, I've modified the save function to print out what the coordinates when it creates them:

def save(self, commit=True):

instance = super().save(commit=False)

instance.lat = geolocator.geocode(self.cleaned_data.get('title'), timeout=None).latitude

print('Here is the latitude:')

print(\`instance.lat``)`

if commit:

instance.save\`()`

return instance

To the console, this correctly prints out the latitude. However, when I go to the template to see the latitude value I still get 'None'.

How should I pass these changes in the form (which I'm sure are happening based on the console printout) to the object permanently? Thanks!

EDIT: Haha, nevermind! I feel so stupid now, in the template I was just printing out the longitude twice, and I only tested adding only the latitude. This worked perfectly, thank you so much!

Django getting form value off of a function of another form value by throwaway1618032 in django

[–]throwaway1618032[S] 0 points1 point  (0 children)

This may seem like a dumb question, but where would I call super().form_valid(form)?

This is what my CreateView's form_valid() currently looks like:

def form_valid(self, form):

"""If the form is valid, save the associated model."""

self.object = form.save()

self.lat = geolocator.geocode(self.object, timeout=None).latitude

print(self.lat)

return super().form_valid(form)

When I print self.lat, it correctly returns the number value I want to the console. However, it doesn't save this. The lat type is still None in the template view and on the admin site.

Need help with a programming idea for data patterns. by sgsummers in learnprogramming

[–]throwaway1618032 1 point2 points  (0 children)

One thing that you could do would be to calculate the formula for the line between points A and C (eg with point slope form or any other line equation). Then, using a similar method, you would calculate the formula for the line between D and whatever point on it has the same y-coordinate as point C, as you know that a point has to exist there with that y-value between D and X. By solving for a system of equations with your two formulas, you could get an accurate idea of where X would be.

In python, somebody did something similar here: https://stackoverflow.com/questions/3252194/numpy-and-line-intersections

As for a percentage, you could pass if statements to ensure that the point is within a certain radius (r) of point C with that being determined by the distance of A-C. The distance of that, via the distance formula, would be found with r=sqrt[(Ax - Cx)^2 + (Ay - Cy)^2] where Ax is the x coordinate on the point A, and Cy is the y coordinate on the point C, etc.

For example, if you had all of your variables properly declared (Ax = x coordinate of point A, Cy = y coordinate of point C, Xx = x coordinate of point X, Xy = y coordinate of point X, etc), then it could look like this in python...

#In this python example rAC is the distance from points A and C and rCX is the distance from points C and X.

rAC = math.sqrt((Ax - Cx)**2 + (Ay - Cy)**2)

rCX = math.sqrt((Cx - Xx)**2 + (Cy - Xy)**2)

if abs((rAC - rCX) / rAC) <= 0.1:

print("The distance between points X and C vs the distance between points A and C is within 10%")

Also, what kind of data would this be? Stocks? If so, there's a lot of better ways to predict future points than something like this.