GDALException OGR failure - geojson

I am working with Django DRF and GeoDjango for a simple model which is as follows.
class Company(models.Model):
name = models.CharField(max_length=200, default='Company', null=True)
def __unicode__(self):
return self.name
class Shop(models.Model):
name = models.CharField(max_length=200, default="bla")
address = models.CharField(max_length=300, default='blabla')
location = models.PointField(null=True, blank=True, geography=True)
company = models.ForeignKey(
Company, on_delete=models.CASCADE, null=True)
This is its serializer.py
class ShopSerializer(serializers.ModelSerializer):
distance = serializers.DecimalField(
source='distance.km', max_digits=10, decimal_places=2, required=False, read_only=True)
class Meta:
model = Shop
fields = ['id', 'name', 'address', 'location', 'distance']
# read_only_fields = ['distance']
class CompanySerializer(serializers.ModelSerializer):
shop_set = ShopSerializer(many=True)
class Meta:
model = Company
fields = ['id', 'name', 'shop_set']
def create(self, validated_data):
shop_validated_data = validated_data.pop('shop_set')
company = Company.objects.create(**validated_data)
shop_set_serializer = self.fields['shop_set']
for each in shop_validated_data:
each['company'] = company
shops = shop_set_serializer.create(shop_validated_data)
return company
everything works fine until I add rest_framework_gis in my settings.py file or add the following line in my shop serializer
serialize('geojson', Shop.objects.all(),
geometry_field='location', fields=('name', 'address'))
In both the cases get GDALException OGR failure.
I have checked my versions of GDAL and Python. Both are 64 bit. And both python and GDAL are working fine.
What I basically need to do here is to convert my POINT field into json lat long response right now the response is as such (if i do not include the lines that cause error).
{
"id": 1,
"name": "Cosmetica",
"address": "somwhere",
"location": "SRID=4326;POINT (24.896 67.182)"
}
Please help.

Related

how to display the fields of related models in the admin panel through relationships model

I'm having trouble with the admin panel. The problem arose due to the presence of relationships model 'ElementVersion'. I don’t know how to display the fields of related models in the admin panel through ElementVersion.
My models.py file
class Refbook(models.Model):
code = models.CharField(max_length=100, unique=True, verbose_name='refbook code')
name = models.CharField(max_length=300, verbose_name='refbook name')
description = models.TextField()
def __str__(self):
return self.name
class Version(models.Model):
refbook_id = models.ForeignKey('Refbook',
on_delete=models.CASCADE,
related_name='versions',
verbose_name='refbook name')
version = models.CharField(max_length=50, verbose_name='refbook version')
date = models.DateField(verbose_name='start date')
def __str__(self):
return self.version
class Meta:
unique_together = [['refbook_id', 'version'], ['refbook_id', 'date']]
class Element(models.Model):
version_id = models.ManyToManyField('Version',
through='ElementVersion',
verbose_name='id_version')
code = models.CharField(max_length=100, unique=True, verbose_name='element's code')
value = models.CharField(max_length=300, verbose_name='element's value')
def __str__(self):
return self.code
class ElementVersion(models.Model):
version = models.ForeignKey(Version,
on_delete=models.CASCADE,
verbose_name='version')
element = models.ForeignKey(Element,
on_delete=models.CASCADE,
verbose_name='element')
class Meta:
unique_together = ('version', 'element')
This is the admin.py file
from .models import Refbook, Version, Element, ElementVersion
class VersionInline(admin.StackedInline):
model = Version
fields = ['date']
readonly_fields = ('date',)
def has_add_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False
#admin.register(Refbook)
class RefbookAdmin(admin.ModelAdmin):
list_display = ['id', 'code', 'name', 'get_last_version', 'get_date_version']
inlines = [VersionInline]
def get_last_version(self, obj):
return obj.versions.values_list('version').last()
get_last_version.short_description = "last version"
def get_date_version(self, obj):
return obj.versions.values_list('date').last()
get_date_version.short_description = "start date"
class ElementVersionInline(admin.TabularInline):
model = ElementVersion
extra = 0
#admin.register(Version)
class VersionAdmin(admin.ModelAdmin):
list_display = ['get_code_refbook', 'refbook_id', 'version', 'date']
inlines = [ElementVersionInline]
def get_code_refbook(self, obj):
return obj.refbook_id.code
get_code_refbook.short_description = 'refbook code'
#admin.register(Element)
class ElementAdmin(admin.ModelAdmin):
list_display = ['id', 'code', 'value']
inlines = [ElementVersionInline]
I want to solve two problems:
on the version editing page, you need the ability to fill in the elements in this version. At the same time, I want all fields with readable names to be displayed, that is, the code and value
on the element editing page, you need the ability to select "reference-version" from the double list

Django Admin prepopulate user's email

I need some help to prepopulate user's email in my models. There has to be an easy way, but i think i am too tired to see the trick. Basically what i need, after the user has been logged in to admin, i need those fields like user, email and name from the User model to be already populated with the info of the current user.
Your help will be much appreciated!
Currently my models are like this:
class ParcareManager(models.Manager):
def active(self, *args, **kwargs):
return super(ParcareManager, self).filter(draft=False).filter(parking_on__lte=timezone.now())
class Parcare(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, default=1, on_delete=True)
email=models.EmailField(blank=True, null=True)
# email = models.EmailField(settings.AUTH_USER_MODEL)
parking_on = models.DateField(auto_now=False, auto_now_add=False,blank=True, null=True)
parking_off = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True) #plecarea din parcare
location =models.CharField(max_length=120, default="P1", blank=True, null=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False,blank=True, null=True)
timestamp=models.DateTimeField(auto_now=False, auto_now_add=True,blank=True, null=True)
venire = models.TimeField(default=datetime.time(9, 00),auto_now=False, auto_now_add=False )
plecare = models.TimeField(default=datetime.time(
18, 00), auto_now=False, auto_now_add=False)
objects = ParcareManager()
def __str__(self):
return self.email + " | " + self.location + " | " + str(self.parking_on) + " | " + str(self.parking_off)
class Meta:
verbose_name_plural = "parcare"
class Meta:
ordering = ["-timestamp", "-updated"]
My admin is like this:
from django.contrib import admin
# Register your models here.
from .models import Parcare
class ParcareModelAdmin(admin.ModelAdmin):
list_display = [ "email","user", "location",
"parking_on", "parking_off", "venire", "plecare"]
list_display_links = ["email", "user" ]
list_editable = [ "parking_off", "parking_on","venire", "plecare"]
list_filter = ["parking_on", "parking_off", "venire", "plecare"]
search_fields = ["location", "name"]
class Meta:
model = Parcare
def email(self, obj):
return obj.user.email
admin.site.register(Parcare, ParcareModelAdmin)
If this is the case, then you may try this:
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj, **kwargs)
if not obj:
user = request.user
form.base_fields['user'].initial = user
form.base_fields['email'].initial = user.email
return form
Here you are fetching the logged in user object from request and then initializing the values from that user.

Django - Autocomplete Light in admin for join Model

So I have a join model defined as follow:
class EventTrack(models.Model):
dj = models.ForeignKey(DjProfile, blank=True)
track = models.ForeignKey(Track, blank=True)
event = models.ForeignKey(Event, blank=True)
def __str__(self):
return '%s - %s' % (self.event, self.track)
Is there a way I can use django-autocomplete-light with this model?
I know how to use it with inline models, but I don't get how to use it with standard field (in this case they are fk though).
At the moment I have the follow, which does not include the autocomplete functionality:
class EventTrackAdmin(admin.ModelAdmin):
fields = ['event', 'dj', 'track']
list_display = ('event', 'dj', 'track')
search_fields = ['event', 'dj', 'track']
admin.site.register(EventTrack, EventTrackAdmin)
edit:
I defined a DjForm as follow:
class DjForm(ModelForm):
dj = ModelChoiceField(
queryset=DjProfile.objects.all(),
widget=autocomplete.ModelSelect2(url='dj-autocomplete')
)
class Meta:
model = DjProfile
fields = '__all__'
Views:
#method_decorator(login_required, name='dispatch')
class DjProfileAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
# Don't forget to filter out results depending on the visitor !
if not self.request.user.is_authenticated():
return DjProfile.objects.none()
qs = DjProfile.objects.all()
if self.q:
qs = qs.filter(name__istartswith=self.q)
return qs
And routing:
from frontend.views import DjProfileAutocomplete
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^dj-autocomplete/$', DjProfileAutocomplete.as_view(create_field='name'), name='dj-autocomplete'),
]
Everything works fine, and if I browse the endpoint I get the json with Djs result. My only problem is to use this in the EventTrack Model, in the admin.
sooo, actually it was easy:
forms.py
class EventTrackForm(ModelForm):
dj = ModelChoiceField(
queryset=DjProfile.objects.all(),
widget=autocomplete.ModelSelect2(url='dj-autocomplete')
)
class Meta:
model = EventTrack
fields = '__all__'
admin.py
class EventTrackAdmin(admin.ModelAdmin):
form = EventTrackForm
...

django admin - how to override the default <select> view for foreign key?

I have 2 django model
class Location
address = models.CharField(max_length=100)
city = models.CharField(max_length=20)
class Client
location = models.ForeignKey(Location)
name = models.CharField(max_length=100)
And I have the following admin.py
class ClientAdmin(admin.ModelAdmin):
fieldsets = [
('Client Details', {'fields': ['name']}),
('Location Details', {'fields': ['location']}),
]
admin.site.register(Client, ClientAdmin)
When I try to add a Client I like to have a editable "location" fields i.e. address and city text fields. django by default gives a list.. how can I get the fields from the foregin tables?
any way I can do this? I don't wish to register the Location table in the admin.
thanks
You could put an inline table for the location and use the method queryset to handle the field values.
So, this is the ideia of the solution:
class LocationInline(admin.StackedInline):
model = Location
def queryset(self, request):
query = ... #your custom query here
qs = super(LocationInline, self).queryset(request).filter(query)
return qs
class ClientAdmin(admin.ModelAdmin):
inlines = [LocationInline]
fieldsets = [
('Client Details', {'fields': ['name']}),
]
Edit:
There is another way to do it in django 1.1.
class ClientAdmin(admin.ModelAdmin):
fieldsets = [
('Client Details', {'fields': ['name']}),
('Location Details', {'fields': ['location']}),
]
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name = "location":
locations = Location.objects.filter( ... ) #your query
kwargs["queryset"] = Location.objects.filter(query)
return super(ClientAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
The docs are here: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey
It's an old question, but answering this can help a lot of other people too.
So, I hope it helps! ;)

Order Django admin change list column by output of __unicode()__ method

Here's part of my Django app's models.py:
class Person(models.Model):
birth_year = WideYear(null=True, blank=True)
birth_year_uncertain = models.BooleanField()
death_year = WideYear(null=True, blank=True)
death_year_uncertain = models.BooleanField()
flourit_year = WideYear(null=True, blank=True)
flourit_year_uncertain = models.BooleanField()
FLOURIT_CHOICES = (
(u'D', u'Birth and death dates'),
(u'F', u'Flourit date'),
)
use_flourit = models.CharField('Date(s) to use', max_length=2, choices=FLOURIT_CHOICES)
index_entries = models.ManyToManyField(IndexEntry, null=True, blank=True)
def __unicode__(self):
if self.personname_set.filter(default_name__exact=True):
name = z(self.personname_set.filter(default_name__exact=True)[0])
else:
name = u'[Unnamed person]'
if self.use_flourit == u'D':
dates = '%s - %s' % (z(self.birth_year), z(self.death_year))
else:
dates = 'fl. ' + z(self.flourit_year)
return '%s (%s)' % (name, dates)
class PersonName(models.Model):
titles = models.CharField(max_length=65535, null=True, blank=True)
surname = models.CharField(max_length=255, null=True, blank=True)
first_name = models.CharField(max_length=255, null=True, blank=True)
middle_names = models.CharField(max_length=255, null=True, blank=True)
post_nominals = models.CharField(max_length=65535, null=True, blank=True)
default_name = models.BooleanField()
person = models.ForeignKey(Person, null=True, blank=True)
def __unicode__(self):
return '%s, %s %s' % (self.surname, self.first_name, self.middle_names)
class Meta:
unique_together = ("titles", "surname", "first_name", "middle_names", "post_nominals", "person")
unique_together = ("default_name", "person")
and here are the corresponding parts of my app's admin.py:
from reversion.admin import VersionAdmin
class PersonNameInline(admin.TabularInline):
model = PersonName
extra = 1
class PersonAdmin(VersionAdmin):
radio_fields = {"use_flourit": admin.HORIZONTAL}
inlines = [PersonNameInline]
admin.site.register(Person, PersonAdmin)
In the admin, this produces a change list as follows:
(source: sampablokuper.com)
As you can see, although the change list populates each row of the Person column with the output of the __unicode()__ method of the Person class, it does not order the rows of that column by the __unicode()__ method of the Person class.
How can I make it do so?
Many thanks in advance!
Django ordering is done in the database level. Unless you store the result of your unicode function in the DB, django is not going to be able to natively return results ordered in that fashion.
Storing an ordering value in the DB is probably the most expedient way to solve this problem.
Just hit the same problem, and I found the following link useful. Instead of sorting by unicode, it tries to sort by multiple columns, which may help solve the problem:
http://djangosnippets.org/snippets/2110/
Following Paul McMillan's suggestion, I added the following line to the class definition of Person:
ordering_string = models.CharField(max_length=255, null=True, blank=True)
I also put the this above the PersonName definition in models.py:
def post_save_person_and_person_name(sender, **kwargs):
person_name = kwargs['instance']
person = person_name.person
if person.ordering_string != unicode(person)[0:254]:
person.ordering_string = unicode(person)[0:254]
super(Person, person).save()
and put this below the PersonName definition in models.py:
post_save.connect(post_save_person_and_person_name, sender=PersonName)
So far, so good.
I think I might be able to improve on it by replacing the save() call above with a queryset update() . I'd welcome suggestions on that front!

Resources