Django get latitude and logitude with django-google-maps
I have a project that uses django-rest-framework, within the project i used madisona/django-google-maps to get latitude and longitude from address, the problem is it's only getting the coordinates in admin.
In django-google-maps there is this widget that uses a javascript:
class GoogleMapsAddressWidget(widgets.TextInput):
"""a widget that will place a google map right after the #id_address field"""
template_name = "django_google_maps/widgets/map_widget.html"
class Media:
css = {
'all': (settings.STATIC_URL +
'django_google_maps/css/google-maps-admin.css', )
}
js = (
'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js',
'https://maps.google.com/maps/api/js?key={}'.format(
settings.GOOGLE_MAPS_API_KEY), settings.STATIC_URL +
'django_google_maps/js/google-maps-admin.js', )
and it requires to override in admin the address field with this:
widgets = {
'address': map_widgets.GoogleMapsAddressWidget
}
How can i get the coordinates when posting from my website when i have a serializer:
models.py
class Location(models.Model):
address = map_fields.AddressField(max_length=200, blank=True, default='')
geolocation = map_fields.GeoLocationField(max_length=100, blank=True, default='')
serializers.py
class Location(serializers.ModelSerializer):
class Meta:
model = Location
fields = ['id', 'address']
/r/django
https://redd.it/7jimux
I have a project that uses django-rest-framework, within the project i used madisona/django-google-maps to get latitude and longitude from address, the problem is it's only getting the coordinates in admin.
In django-google-maps there is this widget that uses a javascript:
class GoogleMapsAddressWidget(widgets.TextInput):
"""a widget that will place a google map right after the #id_address field"""
template_name = "django_google_maps/widgets/map_widget.html"
class Media:
css = {
'all': (settings.STATIC_URL +
'django_google_maps/css/google-maps-admin.css', )
}
js = (
'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js',
'https://maps.google.com/maps/api/js?key={}'.format(
settings.GOOGLE_MAPS_API_KEY), settings.STATIC_URL +
'django_google_maps/js/google-maps-admin.js', )
and it requires to override in admin the address field with this:
widgets = {
'address': map_widgets.GoogleMapsAddressWidget
}
How can i get the coordinates when posting from my website when i have a serializer:
models.py
class Location(models.Model):
address = map_fields.AddressField(max_length=200, blank=True, default='')
geolocation = map_fields.GeoLocationField(max_length=100, blank=True, default='')
serializers.py
class Location(serializers.ModelSerializer):
class Meta:
model = Location
fields = ['id', 'address']
/r/django
https://redd.it/7jimux
<div class="col-3 text-center">
<h5>Child #1: </h4>
</div>
<div class="col-2 text-center">
{{ child.kg }}
</div>
<div class="col-2 text-center" >
{{ child.material }}
</div>
<div class="col-2 text-center" >
{{ child.box_price }}
</div>
<div class="col-3 text-center">
<button type="button" id='remove_box_0' class="btn btn-danger">Remove box</button>
</div>
{% endfor %}
</div>
</div>
</div>
<p>
{{ child_form.management_form }}
<input id="create" class="btn btn-secondary btn-lg btn-block btn-option btn-form" type="submit" value="Crear" />
</p>
javascript
// Adds the difference between desired boxes and current boxes
function add_boxes(form_id, total, num){
for (let i = total; i != total+num; i++) {
var element = $('#nested_forms_' + form_id + "> :first-child ").clone().css('display', 'none');
element.appendTo('#nested_forms_' + form_id).show('250');
};
$('#id_child-TOTAL_FORMS').val(num + total);
};
// Removes every unneeded boxes
function remove_boxes(form_id, total){
$('#nested_forms_' + form_id).children().each(function(){
if ($(this).index() >= total) {
$(this).fadeTo(150, 0).slideUp(150, function(){
$(this).remove();
});
}
});
$('#id_child-TOTAL_FORMS').val(total);
}
/r/django
https://redd.it/8ggdy2
<h5>Child #1: </h4>
</div>
<div class="col-2 text-center">
{{ child.kg }}
</div>
<div class="col-2 text-center" >
{{ child.material }}
</div>
<div class="col-2 text-center" >
{{ child.box_price }}
</div>
<div class="col-3 text-center">
<button type="button" id='remove_box_0' class="btn btn-danger">Remove box</button>
</div>
{% endfor %}
</div>
</div>
</div>
<p>
{{ child_form.management_form }}
<input id="create" class="btn btn-secondary btn-lg btn-block btn-option btn-form" type="submit" value="Crear" />
</p>
javascript
// Adds the difference between desired boxes and current boxes
function add_boxes(form_id, total, num){
for (let i = total; i != total+num; i++) {
var element = $('#nested_forms_' + form_id + "> :first-child ").clone().css('display', 'none');
element.appendTo('#nested_forms_' + form_id).show('250');
};
$('#id_child-TOTAL_FORMS').val(num + total);
};
// Removes every unneeded boxes
function remove_boxes(form_id, total){
$('#nested_forms_' + form_id).children().each(function(){
if ($(this).index() >= total) {
$(this).fadeTo(150, 0).slideUp(150, function(){
$(this).remove();
});
}
});
$('#id_child-TOTAL_FORMS').val(total);
}
/r/django
https://redd.it/8ggdy2
reddit
r/django - Not getting the context data of dynamically-generated formset
0 votes and 0 so far on reddit
Each event should have be able to have mutliple, People and Organisations.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///project.db'
db = SQLAlchemy(app)
class Event(db.Model):
__tablename__ = 'Event'
#id = db.Column(db.Integer, primary_key=True)
person_id = db.Column(db.Integer, db.ForeignKey('Person.id'), primary_key=True)
organisation_id = db.Column(db.Integer, db.ForeignKey('Organisation.id'), primary_key=True)
people = db.relationship('Person', uselist=True, back_populates="events")
organisations = db.relationship('Organisation', uselist=True, back_populates="events")
title = db.Column(db.String(255))
body = db.Column(db.String(255))
class Person(db.Model):
__tablename__ = 'Person'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
password = db.Column(db.String(255))
/r/flask
https://redd.it/10dskrd
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///project.db'
db = SQLAlchemy(app)
class Event(db.Model):
__tablename__ = 'Event'
#id = db.Column(db.Integer, primary_key=True)
person_id = db.Column(db.Integer, db.ForeignKey('Person.id'), primary_key=True)
organisation_id = db.Column(db.Integer, db.ForeignKey('Organisation.id'), primary_key=True)
people = db.relationship('Person', uselist=True, back_populates="events")
organisations = db.relationship('Organisation', uselist=True, back_populates="events")
title = db.Column(db.String(255))
body = db.Column(db.String(255))
class Person(db.Model):
__tablename__ = 'Person'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
password = db.Column(db.String(255))
/r/flask
https://redd.it/10dskrd
reddit
Each event should have be able to have mutliple, People and...
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] =...